// Deferring write until this time allows both points and faces from multiple // point views to be written. void PlyWriter::done(PointTableRef table) { for (auto& v : m_views) { PointRef point(*v, 0); for (PointId idx = 0; idx < v->size(); ++idx) { point.setPointId(idx); writePoint(point, table.layout()); } } if (m_faces) { PointId offset = 0; for (auto& v : m_views) { TriangularMesh *mesh = v->mesh(); if (mesh) { for (size_t id = 0; id < mesh->size(); ++id) { const Triangle& t = (*mesh)[id]; writeTriangle(t, offset); } } offset += v->size(); } } Utils::closeFile(m_stream); m_stream = nullptr; getMetadata().addList("filename", m_filename); }
void LD3dsExporter::writeShapeLine( VertexVector &vecVertices, FaceVector &vecFaces, LDLShapeLine *pShapeLine, const TCFloat *matrix, int colorNumber, bool bfc, bool invert) { if (pShapeLine->getColorNumber() != 16) { colorNumber = pShapeLine->getColorNumber(); } if (shouldFlipWinding(bfc, invert, pShapeLine)) { writeTriangle(vecVertices, vecFaces, pShapeLine->getPoints(), 2, 1, 0, colorNumber, matrix); } else { writeTriangle(vecVertices, vecFaces, pShapeLine->getPoints(), 0, 1, 2, colorNumber, matrix); } if (pShapeLine->getNumPoints() > 3) { if (shouldFlipWinding(bfc, invert, pShapeLine)) { writeTriangle(vecVertices, vecFaces, pShapeLine->getPoints(), 3, 2, 0, colorNumber, matrix); } else { writeTriangle(vecVertices, vecFaces, pShapeLine->getPoints(), 0, 2, 3, colorNumber, matrix); } } }
void ObjPrimitiveIndexWriter::drawArrays(GLenum mode,GLint first,GLsizei count) { switch(mode) { case(GL_TRIANGLES): { unsigned int pos=first; for(GLsizei i=2;i<count;i+=3,pos+=3) { writeTriangle(pos,pos+1,pos+2); } break; } case(GL_TRIANGLE_STRIP): { unsigned int pos=first; for(GLsizei i=2;i<count;++i,++pos) { if ((i%2)) writeTriangle(pos,pos+2,pos+1); else writeTriangle(pos,pos+1,pos+2); } break; } case(GL_QUADS): { unsigned int pos=first; for(GLsizei i=3;i<count;i+=4,pos+=4) { writeTriangle(pos,pos+1,pos+2); writeTriangle(pos,pos+2,pos+3); } break; } case(GL_QUAD_STRIP): { unsigned int pos=first; for(GLsizei i=3;i<count;i+=2,pos+=2) { writeTriangle(pos,pos+1,pos+2); writeTriangle(pos+1,pos+3,pos+2); } break; } case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN case(GL_TRIANGLE_FAN): { unsigned int pos=first+1; for(GLsizei i=2;i<count;++i,++pos) { writeTriangle(first,pos,pos+1); } break; } case(GL_POINTS): { for(GLsizei i=0;i<count;++i) { writePoint(i); } break; } case(GL_LINES): { for(GLsizei i=0;i<count;i+=2) { writeLine(i, i+1); } break; } case(GL_LINE_STRIP): { for(GLsizei i=1;i<count;++i) { writeLine(i-1, i); } break; } case(GL_LINE_LOOP): { for(GLsizei i=1;i<count;++i) { writeLine(i-1, i); } writeLine(count-1, 0); break; } default: OSG_WARN << "OBJWriterNodeVisitor :: can't handle mode " << mode << std::endl; break; } }
template<typename T>void drawElementsImplementation(GLenum mode, GLsizei count, const T* indices) { if (indices==0 || count==0) return; typedef const T* IndexPointer; switch(mode) { case(GL_TRIANGLES): { IndexPointer ilast = &indices[count]; for(IndexPointer iptr=indices;iptr<ilast;iptr+=3) writeTriangle(*iptr,*(iptr+1),*(iptr+2)); break; } case(GL_TRIANGLE_STRIP): { IndexPointer iptr = indices; for(GLsizei i=2;i<count;++i,++iptr) { if ((i%2)) writeTriangle(*(iptr),*(iptr+2),*(iptr+1)); else writeTriangle(*(iptr),*(iptr+1),*(iptr+2)); } break; } case(GL_QUADS): { IndexPointer iptr = indices; for(GLsizei i=3;i<count;i+=4,iptr+=4) { writeTriangle(*(iptr),*(iptr+1),*(iptr+2)); writeTriangle(*(iptr),*(iptr+2),*(iptr+3)); } break; } case(GL_QUAD_STRIP): { IndexPointer iptr = indices; for(GLsizei i=3;i<count;i+=2,iptr+=2) { writeTriangle(*(iptr),*(iptr+1),*(iptr+2)); writeTriangle(*(iptr+1),*(iptr+3),*(iptr+2)); } break; } case(GL_POLYGON): // treat polygons as GL_TRIANGLE_FAN case(GL_TRIANGLE_FAN): { IndexPointer iptr = indices; unsigned int first = *iptr; ++iptr; for(GLsizei i=2;i<count;++i,++iptr) { writeTriangle(first,*(iptr),*(iptr+1)); } break; } case(GL_POINTS): { IndexPointer ilast = &indices[count]; for(IndexPointer iptr=indices;iptr<ilast;++iptr) { writePoint(*iptr); } break; } case(GL_LINES): { IndexPointer ilast = &indices[count]; for(IndexPointer iptr=indices;iptr<ilast;iptr+=2) { writeLine(*iptr, *(iptr+1)); } break; } case(GL_LINE_STRIP): { IndexPointer ilast = &indices[count]; for(IndexPointer iptr=indices+1;iptr<ilast;iptr+=2) { writeLine(*(iptr-1), *iptr); } break; } case(GL_LINE_LOOP): { IndexPointer ilast = &indices[count]; for(IndexPointer iptr=indices+1;iptr<ilast;iptr+=2) { writeLine(*(iptr-1), *iptr); } writeLine(*ilast, *indices); break; } default: // uhm should never come to this point :) break; } }
/* Create GRASS vector output map. Create attribute table. Calculate geometries and write them into the output map. Calculate attributes and write them into the output map's attribute table. */ void writeMap() { int i, j; double xlength, ylength, zlength; double length, flatLength, bailLength; double xoffset, yoffset, zoffset; double xys[12]; int ratio; double zRatio; /* attributes to be written to output map */ int boneID; int skelID; int unitID; int oldID; int cat; char *organization; char buf[MAXSTR]; if ( numPoints < 2 ) { G_fatal_error ("Less than two valid measurement points in input file"); } G_message (_("Constructing geometries for %i valid points:"), numPoints ); /* CREATE OUTPUT VECTOR MAP */ if (Vect_legal_filename(output->answer) < 0) { G_fatal_error(_("Use '%s' option to change vector map name"), output->key); } Map = (struct Map_info *) G_malloc (sizeof ( struct Map_info ) ); if (Vect_open_new(Map, output->answer, WITH_Z) < 0) { G_fatal_error(_("Unable to create vector map <%s>"), output->answer); } Vect_set_map_name(Map, output->answer); Vect_hist_command(Map); if ((organization = getenv("GRASS_ORGANIZATION"))) { Vect_set_organization(Map, organization); } else { Vect_set_organization(Map, "UNKNOWN ORGANIZATION"); } Vect_set_date(Map, G_date()); Vect_set_person(Map, G_whoami()); Vect_set_map_date(Map, ""); Vect_set_scale(Map, 2400); Vect_set_comment(Map, ""); Vect_set_zone(Map, 0); Vect_set_thresh(Map, 0.0); /* START DBMS INTERFACE */ /* prepare strings for use in db_* calls */ db_init_string(&sql); /* start default database driver */ Fi = Vect_default_field_info(Map, 1, NULL, GV_1TABLE); driver = db_start_driver_open_database(Fi->driver,Vect_subst_var(Fi->database, Map)); if (driver == NULL) { Vect_delete(output->answer); G_fatal_error(_("Unable to open database <%s> by driver <%s>"), Vect_subst_var(Fi->database, Map), Fi->driver); } /* create attribute table */ db_begin_transaction ( driver ); sprintf(buf, "create table %s (cat integer, skel_id integer, bone_id integer, unit_id integer, GRASSRGB varchar(11),BONERGB varchar(11));", Fi->table); if ( DEBUG ) { fprintf ( stderr, "Creating attribute table: %s\n", buf ); } db_set_string(&sql, buf); if (db_execute_immediate(driver, &sql) != DB_OK) { Vect_delete(output->answer); G_fatal_error(_("Unable to create attribute table: %s"), db_get_string(&sql)); } if (db_grant_on_table (driver, output->answer, DB_PRIV_SELECT, DB_GROUP | DB_PUBLIC) != DB_OK) { Vect_delete(output->answer); G_fatal_error(_("Unable to grant privileges on table <%s>"), output->answer); } if (db_create_index2(driver, output->answer, "cat") != DB_OK) { G_warning(_("Unable to create index for table <%s>, key <%s>"), output->answer, "cat"); } /* link vector map to attribute table */ if (Vect_map_add_dblink(Map, 1, NULL, Fi->table, "cat", Fi->database, Fi->driver) ) { Vect_delete(output->answer); G_fatal_error(_("Unable to add database link for vector map <%s>"), Vect_get_full_name(Map)); } /* PROCESS POINTS AND WRITE GEOMETRIES */ /* Now process point measurements and write geometries into output vector map. */ /* At this stage, the global points array has an even number of valid points. */ oldID = pointTable[0].SKEL_ID; unitID = 1; cat = 0; for ( i = 0; i < numPoints; i = i + 2 ) { /* This boneID is a generalized ID that does not differentiate between start and end measurement. */ boneID = (int) pointTable[i+1].BONE_ID / 2; skelID = pointTable[i+1].SKEL_ID; /* get coordinates for top and bottom of bone */ ax = pointTable[i].X; ay = pointTable[i].Y; az = pointTable[i].Z; bx = pointTable[i+1].X; by = pointTable[i+1].Y; bz = pointTable[i+1].Z; /* get vector lengths */ xlength = fabs (ax - bx); ylength = fabs (ay - by); zlength = fabs (az - bz); /* get real length */ length = sqrt ( (xlength*xlength) + (ylength*ylength) + (zlength*zlength) ); /* get length in x/y plane */ flatLength = sqrt ( (xlength*xlength) + (ylength*ylength) ); /* determine ratio for triangles, depending on bone type */ ratio = 12; /* default */ for ( j = 0; j < NUM_RATIOS; j ++ ) { if ( boneID == RATIO_ID[j] ) { ratio = RATIO_VAL[j]; } } /* get bail length */ bailLength = (double) ( length / (double) ratio); /* calculate bail offsets from top point (one bail is mirror of the other) */ xoffset = (bailLength * ylength) / flatLength; yoffset = ( (bailLength * xlength) / flatLength ) * (-1); zoffset = 0; xys[0]= ax + xoffset; xys[1]= ay + yoffset; xys[2]= az + zoffset; xys[6]= ax - xoffset; xys[7]= ay - yoffset; xys[8]= az - zoffset; /* get 3rd axis offsets */ zRatio = (zlength/ratio) / flatLength; xoffset = xlength * zRatio; yoffset = ylength * zRatio; zoffset = (flatLength/ratio) * (-1); xys[3]= ax + xoffset; xys[4]= ay + yoffset; xys[5]= az + zoffset; xys[9]= ax - xoffset; xys[10]= ay - yoffset; xys[11]= az - zoffset; /* Increase unit ID by "1", if we have another skeleton ID */ if ( oldID != pointTable[i+1].SKEL_ID ) { unitID ++; oldID = pointTable[i+1].SKEL_ID; /* switch to next colour for next geometry */ RGBNUM ++; if ( RGBNUM == RGBMAX ) { RGBNUM = 0; } } /* write geometries */ if ( MODE == MODE_DARTS ) { writeTriangle ( cat, skelID, boneID, unitID, xys, 0, 6 ); cat ++; writeTriangle ( cat, skelID, boneID, unitID, xys, 3, 9 ); cat ++; } if ( MODE == MODE_LINES ) { writeLine ( cat, skelID, boneID, unitID ); cat ++; } if ( MODE == MODE_PLANES_H ) { writeTriangle ( cat, skelID, boneID, unitID, xys, 0, 6 ); cat ++; } if ( MODE == MODE_PLANES_V ) { writeTriangle ( cat, skelID, boneID, unitID, xys, 3, 9 ); cat ++; } if ( MODE == MODE_POINTS ) { writePoints ( cat, skelID, boneID, unitID ); cat = cat + 2; } if ( MODE == MODE_PYRAMIDS ) { writeTriangle ( cat, skelID, boneID, unitID, xys, 0, 3 ); cat ++; writeTriangle ( cat, skelID, boneID, unitID, xys, 3, 6 ); cat ++; writeTriangle ( cat, skelID, boneID, unitID, xys, 6, 9 ); cat ++; writeTriangle ( cat, skelID, boneID, unitID, xys, 9, 0 ); cat ++; writeSquare ( cat, skelID, boneID, unitID, xys ); cat ++; } /* switch to next colour for bone colouring */ RGBNUM_BONE ++; if ( RGBNUM_BONE == RGBMAX ) { RGBNUM_BONE = 0; } G_percent ( i, numPoints - 2, 1 ); } fprintf ( stdout, "\n" ); /* commit DBMS actions */ db_commit_transaction(driver); db_close_database_shutdown_driver(driver); if (!Vect_build(Map)) { G_warning("Building topology failed"); } Vect_close(Map); db_free_string(&sql); }