Permalink
Browse files

Added NSXMLElement (COLLADAParser) category containing -trimmedString…

…Value method.

Adapted to use NSXMLElement (COLLADAParser) -trimmedStringValue instead of NSXMLElement -stringValue to avoid occurrences of empty strings in arrays created via NSString -componentsSeparatedByString:@" ". Extra empty strings throw off the count of the number of indices and coordinates in parsed COLLADA elements.
Added support for parsing COLLADA <polygons> element using the assumption that polygons are actually all triangles. The current implementation will likely break badly if non-triangular <polygons> elements are parsed.
  • Loading branch information...
1 parent 2b16b19 commit dc76a097dd51667ad451dcea9471841b3210b83b @erikbuck committed Oct 29, 2012
Showing with 44 additions and 5 deletions.
  1. +7 −0 COLLADAViewer2/COLLADA/COLLADAParser+geometry.h
  2. +37 −5 COLLADAViewer2/COLLADA/COLLADAParser+geometry.m
@@ -17,3 +17,10 @@
(NSXMLElement *)element;
@end
+
+
+@interface NSXMLNode (COLLADAParser)
+
+- (NSString *)trimmedStringValue;
+
+@end
@@ -153,7 +153,7 @@ - (NSData *)extractFloatArrayFromElement:(NSXMLElement *)element
// element changed to <float_array>
element = [floatArrays lastObject];
- NSArray *values = [[element stringValue]
+ NSArray *values = [[element trimmedStringValue]
componentsSeparatedByString:@" "];
NSMutableData *floatData = [NSMutableData data];
@@ -346,7 +346,7 @@ - (COLLADATrianglesInfo *)extractTrianglesInfoFromElement:
NSArray *indices = [triangle elementsForName:@"p"];
for(NSXMLElement *p in indices)
{
- NSArray *values = [[p stringValue]
+ NSArray *values = [[p trimmedStringValue]
componentsSeparatedByString:@" "];
for(NSString *value in values)
@@ -363,7 +363,7 @@ - (COLLADATrianglesInfo *)extractTrianglesInfoFromElement:
/////////////////////////////////////////////////////////////////
-// Only 3 sides polygons are supported so polylist is synonomous
+// Only 3 sided polygons are supported so polylist is synonomous
// with triangles (hopefully!)
- (COLLADATrianglesInfo *)extractPolylistInfoFromElement:
(NSXMLElement *)polylist;
@@ -425,7 +425,7 @@ - (COLLADATrianglesInfo *)extractPolylistInfoFromElement:
NSArray *indices = [polylist elementsForName:@"p"];
for(NSXMLElement *p in indices)
{
- NSArray *values = [[p stringValue]
+ NSArray *values = [[p trimmedStringValue]
componentsSeparatedByString:@" "];
for(NSString *value in values)
@@ -473,6 +473,7 @@ - (COLLADAMeshGeometry *)extractGeometryFromGeometryElement:
NSArray *vertices = [mesh elementsForName:@"vertices"];
NSArray *triangles = [mesh elementsForName:@"triangles"];
NSArray *polylists = [mesh elementsForName:@"polylist"];
+ NSArray *polygons = [mesh elementsForName:@"polygons"];
NSArray *lines = [mesh elementsForName:@"lines"];
NSMutableDictionary *mutableSourcesByID =
[NSMutableDictionary dictionary];
@@ -535,13 +536,29 @@ - (COLLADAMeshGeometry *)extractGeometryFromGeometryElement:
vertices:mutableVertexInfoByID];
}
}
+ else if(nil != polygons && 0 < [polygons count])
+ {
+ // Parse each <polygons> element
+ for(NSXMLElement *polygon in polygons)
+ { // polygon is <p>
+ meshTrianglesInfo =
+ [self extractTrianglesInfoFromElement:polygon];
+ NSAssert(nil != meshTrianglesInfo,
+ @"Invalid <triangles>");
+
+ [meshGeometry appendTriangles:meshTrianglesInfo
+ sources:mutableSourcesByID
+ vertices:mutableVertexInfoByID];
+ }
+ }
else if(nil != lines && 0 < [lines count])
{ // Lines currently ignored
// NSLog(@"Geometry <lines> ignored");
}
else
{
- NSLog(@"Mesh has niether <triangles> nor <polylist>");
+ NSLog(@"Mesh has none of %@",
+ @"<triangles>, <polylist>, <polygons>, or <lines>");
}
} // <mesh>
@@ -788,3 +805,18 @@ @implementation COLLADASourceInfo
@end
+
+
+/////////////////////////////////////////////////////////////////
+//
+@implementation NSXMLNode (COLLADAParser)
+
+/////////////////////////////////////////////////////////////////
+//
+- (NSString *)trimmedStringValue;
+{
+ return [[self stringValue] stringByTrimmingCharactersInSet:
+ [NSCharacterSet whitespaceAndNewlineCharacterSet]];
+}
+
+@end

0 comments on commit dc76a09

Please sign in to comment.