void Toroid::init(){
    vertCount = detail * segs;
    triFaceCount = vertCount*2;
    
    indicesCount = triFaceCount*3;
    
    allocateMemory();
    createVertices();
    createIndices();
    createFaces();
    createVertexNormals();
    createCoords();
}
void Centerline::cutMesh()
{
  Msg::Info("Centerline: action (cutMesh) splits surface mesh (%d tris) using %s ",
            triangles.size(), fileName.c_str());

  //splitMesh
  for(unsigned int i = 0; i < edges.size(); i++){
    std::vector<MLine*> lines = edges[i].lines;
    double L = edges[i].length;
    double D = 2.*edges[i].minRad;  //(edges[i].minRad+edges[i].maxRad);
    double AR = L/D;
    // printf("*** Centerline branch %d (AR=%.1f) \n", edges[i].tag, AR);

    int nbSplit = (int)ceil(AR/2 + 1.1); //AR/2 + 0.9
    if( nbSplit > 1 ){
      //printf("->> cut branch in %d parts \n",  nbSplit);
      double li  = L/nbSplit;
      double lc = 0.0;
      for (unsigned int j= 0; j < lines.size(); j++){
	lc += lines[j]->getLength();
	if (lc > li && nbSplit > 1) {
	  MVertex *v1 = lines[j]->getVertex(0);
	  MVertex *v2 = lines[j]->getVertex(1);
	  SVector3 pt(v1->x(), v1->y(), v1->z());
	  SVector3 dir(v2->x()-v1->x(),v2->y()-v1->y(),v2->z()-v1->z());
	  std::map<MLine*,double>::iterator itr = radiusl.find(lines[j]);
	  cutByDisk(pt, dir, itr->second);
	  nbSplit--;
	  lc = 0.0;
	}
      }
    }
    if(edges[i].children.size() > 0.0 && AR > 1.0){
      MVertex *v1 = lines[lines.size()-1]->getVertex(1);//end vertex
      MVertex *v2;
      if(AR < 1.5) v2 = lines[0]->getVertex(0);
      else if (lines.size() > 4) v2 = lines[lines.size()-4]->getVertex(0);
      else v2 = lines[lines.size()-1]->getVertex(0);
      SVector3 pt(v1->x(), v1->y(), v1->z());
      SVector3 dir(v2->x()-v1->x(),v2->y()-v1->y(),v2->z()-v1->z());
      //printf("-->> cut branch at bifurcation \n");
      std::map<MLine*,double>::iterator itr = radiusl.find(lines[lines.size()-1]);
      //bool cutted =
      cutByDisk(pt, dir, itr->second);
      // if(!cutted){
      //   int l = lines.size()-1-lines.size()/(4*nbSplit); //chech this!
      //   v1 = lines[l]->getVertex(1);
      //   v2 = lines[l]->getVertex(0);
      //   pt = SVector3(v1->x(), v1->y(), v1->z());
      //   dir = SVector3(v2->x()-v1->x(),v2->y()-v1->y(),v2->z()-v1->z());
      //   printf("-->> cut bifurcation NEW \n");
      //   itr = radiusl.find(lines[l]);
      //   cutted = cutByDisk(pt, dir, itr->second);
      // }
    }
 }

  //create discreteFaces
  createFaces();
  current->createTopologyFromFaces(discFaces);
  current->exportDiscreteGEOInternals();

  //write
  Msg::Info("Centerline: writing splitted mesh 'myPARTS.msh'");
  current->writeMSH("myPARTS.msh", 2.2, false, false);

  //create compounds
  createSplitCompounds();

  Msg::Info("Done splitting mesh by centerlines");
}
Exemple #3
0
bool ModelNode_NWN2::loadSkin(Model_NWN2::ParserContext &ctx) {
	if (ctx.mdb->readUint32BE() != kSkinID)
		throw Common::Exception("Packet signatures do not match");

	uint32 packetSize = ctx.mdb->readUint32LE();

	_name.readFixedASCII(*ctx.mdb, 32);

	// Skipping lower level of detail models
	if (_name.endsWith("_L01") || _name.endsWith("_L02"))
		return false;

	Common::UString skeletonName;
	skeletonName.readFixedASCII(*ctx.mdb, 32);

	Common::UString diffuseMap, normalMap, tintMap, glowMap;
	diffuseMap.readFixedASCII(*ctx.mdb, 32);
	 normalMap.readFixedASCII(*ctx.mdb, 32);
	   tintMap.readFixedASCII(*ctx.mdb, 32);
	   glowMap.readFixedASCII(*ctx.mdb, 32);

	_diffuse [0] = ctx.mdb->readIEEEFloatLE();
	_diffuse [1] = ctx.mdb->readIEEEFloatLE();
	_diffuse [2] = ctx.mdb->readIEEEFloatLE();
	_specular[0] = ctx.mdb->readIEEEFloatLE();
	_specular[1] = ctx.mdb->readIEEEFloatLE();
	_specular[2] = ctx.mdb->readIEEEFloatLE();

	float  specularPower = ctx.mdb->readIEEEFloatLE();
	float  specularValue = ctx.mdb->readIEEEFloatLE();
	uint32 textureFlags  = ctx.mdb->readUint32LE();

	uint32 vertexCount = ctx.mdb->readUint32LE();
	uint32 faceCount   = ctx.mdb->readUint32LE();

	if ((vertexCount == 0) || (faceCount == 0))
		return false;

	std::vector<Common::UString> textures;
	textures.push_back(diffuseMap);

	uint32 textureCount = textures.size();

	loadTextures(textures);
	if (!createFaces(faceCount))
		return false;


	// Read vertex coordinates

	std::vector<float> vX, vY, vZ;
	vX.resize(vertexCount);
	vY.resize(vertexCount);
	vZ.resize(vertexCount);

	std::vector<float> tX, tY, tZ;
	tX.resize(vertexCount);
	tY.resize(vertexCount);
	tZ.resize(vertexCount);

	for (uint32 i = 0; i < vertexCount; i++) {
		vX[i] = ctx.mdb->readIEEEFloatLE();
		vY[i] = ctx.mdb->readIEEEFloatLE();
		vZ[i] = ctx.mdb->readIEEEFloatLE();

		ctx.mdb->skip(3 * 4); // Normals

		ctx.mdb->skip(4 * 4); // Bone weights
		ctx.mdb->skip(4 * 1); // Bone indices

		ctx.mdb->skip(3 * 4); // Tangents
		ctx.mdb->skip(3 * 4); // Binormals

		tX[i] = ctx.mdb->readIEEEFloatLE();
		tY[i] = ctx.mdb->readIEEEFloatLE();
		tZ[i] = ctx.mdb->readIEEEFloatLE();

		ctx.mdb->skip(4); // Bone count
	}


	// Read faces

	for (uint32 i = 0; i < faceCount; i++) {
		const uint16 v1 = ctx.mdb->readUint16LE();
		const uint16 v2 = ctx.mdb->readUint16LE();
		const uint16 v3 = ctx.mdb->readUint16LE();

		// Vertex coordinates
		_vX[3 * i + 0] = v1 < vX.size() ? vX[v1] : 0.0;
		_vY[3 * i + 0] = v1 < vY.size() ? vY[v1] : 0.0;
		_vZ[3 * i + 0] = v1 < vZ.size() ? vZ[v1] : 0.0;
		_boundBox.add(_vX[3 * i + 0], _vY[3 * i + 0], _vZ[3 * i + 0]);

		_vX[3 * i + 1] = v2 < vX.size() ? vX[v2] : 0.0;
		_vY[3 * i + 1] = v2 < vY.size() ? vY[v2] : 0.0;
		_vZ[3 * i + 1] = v2 < vZ.size() ? vZ[v2] : 0.0;
		_boundBox.add(_vX[3 * i + 1], _vY[3 * i + 1], _vZ[3 * i + 1]);

		_vX[3 * i + 2] = v3 < vX.size() ? vX[v3] : 0.0;
		_vY[3 * i + 2] = v3 < vY.size() ? vY[v3] : 0.0;
		_vZ[3 * i + 2] = v3 < vZ.size() ? vZ[v3] : 0.0;
		_boundBox.add(_vX[3 * i + 2], _vY[3 * i + 2], _vZ[3 * i + 2]);

		// Texture coordinates
		for (uint32 t = 0; t < textureCount; t++) {
			_tX[3 * textureCount * i + 3 * t + 0] = v1 < tX.size() ? tX[v1] : 0.0;
			_tY[3 * textureCount * i + 3 * t + 0] = v1 < tY.size() ? tY[v1] : 0.0;

			_tX[3 * textureCount * i + 3 * t + 1] = v2 < tX.size() ? tX[v2] : 0.0;
			_tY[3 * textureCount * i + 3 * t + 1] = v2 < tY.size() ? tY[v2] : 0.0;

			_tX[3 * textureCount * i + 3 * t + 2] = v3 < tX.size() ? tX[v3] : 0.0;
			_tY[3 * textureCount * i + 3 * t + 2] = v3 < tY.size() ? tY[v3] : 0.0;
		}

	}

	createCenter();

	_render = true;

	return true;
}
Exemple #4
0
void ModelNode_Witcher::readMesh(Model_Witcher::ParserContext &ctx) {
	ctx.mdb->skip(8);

	uint32 offMeshArrays = ctx.mdb->readUint32LE();

	ctx.mdb->skip(76);

	_ambient [0] = ctx.mdb->readIEEEFloatLE();
	_ambient [1] = ctx.mdb->readIEEEFloatLE();
	_ambient [2] = ctx.mdb->readIEEEFloatLE();
	_diffuse [0] = ctx.mdb->readIEEEFloatLE();
	_diffuse [1] = ctx.mdb->readIEEEFloatLE();
	_diffuse [2] = ctx.mdb->readIEEEFloatLE();
	_specular[0] = ctx.mdb->readIEEEFloatLE();
	_specular[1] = ctx.mdb->readIEEEFloatLE();
	_specular[2] = ctx.mdb->readIEEEFloatLE();

	_shininess = ctx.mdb->readIEEEFloatLE();

	ctx.mdb->skip(20);

	Common::UString texture[4];
	texture[0].readFixedASCII(*ctx.mdb, 64);
	texture[1].readFixedASCII(*ctx.mdb, 64);
	texture[2].readFixedASCII(*ctx.mdb, 64);
	texture[3].readFixedASCII(*ctx.mdb, 64);

	ctx.mdb->skip(20);

	uint32 fourCC = ctx.mdb->readUint32BE();

	ctx.mdb->skip(8);

	float coronaCenterX = ctx.mdb->readIEEEFloatLE();

	ctx.mdb->skip(8);

	float enlargeStartDistance = ctx.mdb->readIEEEFloatLE();

	ctx.mdb->skip(308);

	ctx.offTextureInfo = ctx.mdb->readUint32LE();

	ctx.mdb->skip(4);

	uint32 endPos = ctx.mdb->seekTo(ctx.offRawData + offMeshArrays);

	ctx.mdb->skip(4);

	uint32 verticesOffset, verticesCount;
	Model::readArrayDef(*ctx.mdb, verticesOffset, verticesCount);

	uint32 normalsOffset, normalsCount;
	Model::readArrayDef(*ctx.mdb, normalsOffset, normalsCount);

	uint32 tangentsOffset, tangentsCount;
	Model::readArrayDef(*ctx.mdb, tangentsOffset, tangentsCount);

	uint32 biNormalsOffset, biNormalsCount;
	Model::readArrayDef(*ctx.mdb, biNormalsOffset, biNormalsCount);

	uint32 tVerts0Offset, tVerts0Count;
	Model::readArrayDef(*ctx.mdb, tVerts0Offset, tVerts0Count);

	uint32 tVerts1Offset, tVerts1Count;
	Model::readArrayDef(*ctx.mdb, tVerts1Offset, tVerts1Count);

	uint32 tVerts2Offset, tVerts2Count;
	Model::readArrayDef(*ctx.mdb, tVerts2Offset, tVerts2Count);

	uint32 tVerts3Offset, tVerts3Count;
	Model::readArrayDef(*ctx.mdb, tVerts3Offset, tVerts3Count);

	uint32 unknownOffset, unknownCount;
	Model::readArrayDef(*ctx.mdb, unknownOffset, unknownCount);

	uint32 facesOffset, facesCount;
	Model::readArrayDef(*ctx.mdb, facesOffset, facesCount);

	if (ctx.fileVersion == 133)
		ctx.offTexData = ctx.mdb->readUint32LE();


	if ((verticesCount == 0) || (facesCount == 0)) {
		ctx.mdb->seekTo(endPos);
		return;
	}

	_render = true;

	std::vector<Common::UString> textures;
	readTextures(ctx, texture[0], textures);
	loadTextures(textures);

	uint32 textureCount = textures.size();

	if (!createFaces(facesCount)) {
		ctx.mdb->seekTo(endPos);
		return;
	}



	// Read vertex coordinates
	ctx.mdb->seekTo(ctx.offRawData + verticesOffset);

	std::vector<float> vX, vY, vZ;
	vX.resize(verticesCount);
	vY.resize(verticesCount);
	vZ.resize(verticesCount);

	for (uint32 i = 0; i < verticesCount; i++) {
		vX[i] = ctx.mdb->readIEEEFloatLE();
		vY[i] = ctx.mdb->readIEEEFloatLE();
		vZ[i] = ctx.mdb->readIEEEFloatLE();
	}


	// Read texture coordinates

	ctx.mdb->seekTo(ctx.offRawData + tVerts0Offset);

	std::vector<float> tX, tY;
	tX.resize(tVerts0Count);
	tY.resize(tVerts0Count);

	for (uint32 i = 0; i < tVerts0Count; i++) {
		tX[i] = ctx.mdb->readIEEEFloatLE();
		tY[i] = ctx.mdb->readIEEEFloatLE();
	}


	// Read faces

	ctx.mdb->seekTo(ctx.offRawData + facesOffset);

	for (uint32 i = 0; i < facesCount; i++) {
		ctx.mdb->skip(4 * 4 + 4);

		if (ctx.fileVersion == 133)
			ctx.mdb->skip(3 * 4);

		// Vertex indices
		const uint32 v1 = ctx.mdb->readUint32LE();
		const uint32 v2 = ctx.mdb->readUint32LE();
		const uint32 v3 = ctx.mdb->readUint32LE();

		// Vertex coordinates
		_vX[3 * i + 0] = v1 < vX.size() ? vX[v1] : 0.0;
		_vY[3 * i + 0] = v1 < vY.size() ? vY[v1] : 0.0;
		_vZ[3 * i + 0] = v1 < vZ.size() ? vZ[v1] : 0.0;
		_boundBox.add(_vX[3 * i + 0], _vY[3 * i + 0], _vZ[3 * i + 0]);

		_vX[3 * i + 1] = v2 < vX.size() ? vX[v2] : 0.0;
		_vY[3 * i + 1] = v2 < vY.size() ? vY[v2] : 0.0;
		_vZ[3 * i + 1] = v2 < vZ.size() ? vZ[v2] : 0.0;
		_boundBox.add(_vX[3 * i + 1], _vY[3 * i + 1], _vZ[3 * i + 1]);

		_vX[3 * i + 2] = v3 < vX.size() ? vX[v3] : 0.0;
		_vY[3 * i + 2] = v3 < vY.size() ? vY[v3] : 0.0;
		_vZ[3 * i + 2] = v3 < vZ.size() ? vZ[v3] : 0.0;
		_boundBox.add(_vX[3 * i + 2], _vY[3 * i + 2], _vZ[3 * i + 2]);

		const float tX1 = v1 < tX.size() ? tX[v1] : 0.0;
		const float tY1 = v1 < tY.size() ? tY[v1] : 0.0;

		const float tX2 = v2 < tX.size() ? tX[v2] : 0.0;
		const float tY2 = v2 < tY.size() ? tY[v2] : 0.0;

		const float tX3 = v3 < tX.size() ? tX[v3] : 0.0;
		const float tY3 = v3 < tY.size() ? tY[v3] : 0.0;

		for (uint32 t = 0; t < textureCount; t++) {
			_tX[3 * textureCount * i + 3 * t + 0] = tX1;
			_tY[3 * textureCount * i + 3 * t + 0] = tY1;

			_tX[3 * textureCount * i + 3 * t + 1] = tX2;
			_tY[3 * textureCount * i + 3 * t + 1] = tY2;

			_tX[3 * textureCount * i + 3 * t + 2] = tX3;
			_tY[3 * textureCount * i + 3 * t + 2] = tY3;
		}

		if (ctx.fileVersion == 133)
			ctx.mdb->skip(4);
	}

	createCenter();

	ctx.mdb->seekTo(endPos);
}