//----------------------------------------------------------------------- /// <summary> /// 查找交集 /// </summary> public void _calcIntersections() { mIntersectionsMap.Clear(); mIntersections.Clear(); //std::map<Ogre::Vector3, PathIntersection, Vector3Comparator> pointSet; std_map <Vector3, std_vector <PathCoordinate> > pointSet = new std_map <Vector3, std_vector <PathCoordinate> >(new Vector3Comparator()); // for (std::vector<Path>::iterator it = mPaths.begin(); it!= mPaths.end(); ++it) uint it_index = 0; foreach (var it in mPaths) { uint it_point_index = 0; //for (std::vector<Ogre::Vector3>::const_iterator it2 = it->getPoints().begin(); it2 != it->getPoints().end(); ++it2) foreach (var it2 in it._getPoints()) { //PathCoordinate pc(it-mPaths.begin(), it2-it->getPoints().begin()); PathCoordinate pc = new PathCoordinate(it_index, it_point_index); //if (pointSet.find(*it2)==pointSet.end()) if (!pointSet.ContainsKey(it2)) { std_vector <PathCoordinate> pi = new std_vector <PathCoordinate>(); pi.Add(pc); //pointSet[it2] = pi; pointSet.Add(it2, pi); } else { pointSet[it2].push_back(pc); } it_point_index++; } it_index++; } //for (std::map<Ogre::Vector3, PathIntersection, Vector3Comparator>::iterator it = pointSet.begin(); it != pointSet.end(); ++it) foreach (var it_ps in pointSet) { if (it_ps.Value.size() > 1) { foreach (var it2 in it_ps.Value) { //mIntersectionsMap[*it2] = it->second; if (mIntersectionsMap.ContainsKey(it2)) { mIntersectionsMap[it2] = it_ps.Value; } else { mIntersectionsMap.Add(it2, it_ps.Value); } } mIntersections.push_back(it_ps.Value); } } // }
// * // * Builds the mesh into the given TriangleBuffer // * @param buffer The TriangleBuffer on where to append the mesh. // // //ORIGINAL LINE: void addToTriangleBuffer(TriangleBuffer& buffer) const public override void addToTriangleBuffer(ref TriangleBuffer buffer) { std_vector <Vector3> vertices = new std_vector <Vector3>(); int offset = 0; /// Step 1 : Generate icosahedron float phi = 0.5f * (1.0f + Math.Sqrt(5.0f)); float invnorm = 1f / Math.Sqrt(phi * phi + 1f); vertices.push_back(invnorm * new Vector3(-1f, phi, 0f)); //0 vertices.push_back(invnorm * new Vector3(1f, phi, 0f)); //1 vertices.push_back(invnorm * new Vector3(0f, 1f, -phi)); //2 vertices.push_back(invnorm * new Vector3(0f, 1f, phi)); //3 vertices.push_back(invnorm * new Vector3(-phi, 0f, -1f)); //4 vertices.push_back(invnorm * new Vector3(-phi, 0f, 1f)); //5 vertices.push_back(invnorm * new Vector3(phi, 0f, -1f)); //6 vertices.push_back(invnorm * new Vector3(phi, 0f, 1f)); //7 vertices.push_back(invnorm * new Vector3(0f, -1f, -phi)); //8 vertices.push_back(invnorm * new Vector3(0f, -1f, phi)); //9 vertices.push_back(invnorm * new Vector3(-1f, -phi, 0f)); //10 vertices.push_back(invnorm * new Vector3(1f, -phi, 0f)); //11 int[] firstFaces = { 0, 1, 2, 0, 3, 1, 0, 4, 5, 1, 7, 6, 1, 6, 2, 1, 3, 7, 0, 2, 4, 0, 5, 3, 2, 6, 8, 2, 8, 4, 3, 5, 9, 3, 9, 7, 11, 6, 7, 10, 5, 4, 10, 4, 8, 10, 9, 5, 11, 8, 6, 11, 7, 9, 10, 8, 11, 10, 11, 9 }; //C++ TO C# CONVERTER WARNING: This 'sizeof' ratio was replaced with a direct reference to the array length: //ORIGINAL LINE: std::vector<int> faces(firstFaces, firstFaces + sizeof(firstFaces)/sizeof(*firstFaces)); // 定义一个容纳100个int型数据的容器,初值赋为0 //vector<int> vecMyHouse(100,0); std_vector <int> faces = new std_vector <int>(firstFaces);//(firstFaces, firstFaces + firstFaces.Length); int size = 60; /// Step 2 : tessellate for (ushort iteration = 0; iteration < mNumIterations; iteration++) { size *= 4; std_vector <int> newFaces = new std_vector <int>(); newFaces.Clear(); //newFaces.resize(size); for (int i = 0; i < size / 12; i++) { int i1 = faces[i * 3]; int i2 = faces[i * 3 + 1]; int i3 = faces[i * 3 + 2]; int i12 = vertices.Count; int i23 = i12 + 1; int i13 = i12 + 2; Vector3 v1 = vertices[i1]; Vector3 v2 = vertices[i2]; Vector3 v3 = vertices[i3]; //make 1 vertice at the center of each edge and project it onto the sphere vertices.push_back((v1 + v2).NormalisedCopy); vertices.push_back((v2 + v3).NormalisedCopy); vertices.push_back((v1 + v3).NormalisedCopy); //now recreate indices newFaces.push_back(i1); newFaces.push_back(i12); newFaces.push_back(i13); newFaces.push_back(i2); newFaces.push_back(i23); newFaces.push_back(i12); newFaces.push_back(i3); newFaces.push_back(i13); newFaces.push_back(i23); newFaces.push_back(i12); newFaces.push_back(i23); newFaces.push_back(i13); } //faces.swap(newFaces); faces = newFaces; } /// Step 3 : generate texcoords std_vector <Vector2> texCoords = new std_vector <Vector2>(); for (ushort i = 0; i < vertices.size(); i++) { Vector3 vec = vertices[i]; float u = 0f; float v = 0f; float r0 = sqrtf(vec.x * vec.x + vec.z * vec.z); float alpha = 0f; alpha = atan2f(vec.z, vec.x); u = alpha / Math.TWO_PI + .5f; v = atan2f(vec.y, r0) / Math.PI + .5f; texCoords.push_back(new Vector2(u, v)); } /// Step 4 : fix texcoords // find vertices to split std_vector <int> indexToSplit = new std_vector <int>(); for (int i = 0; i < faces.size() / 3; i++) { Vector2 t0 = texCoords[faces[i * 3 + 0]]; Vector2 t1 = texCoords[faces[i * 3 + 1]]; Vector2 t2 = texCoords[faces[i * 3 + 2]]; if (Math.Abs(t2.x - t0.x) > 0.5) { if (t0.x < 0.5) { indexToSplit.push_back(faces[i * 3]); } else { indexToSplit.push_back(faces[i * 3 + 2]); } } if (Math.Abs(t1.x - t0.x) > 0.5) { if (t0.x < 0.5) { indexToSplit.push_back(faces[i * 3]); } else { indexToSplit.push_back(faces[i * 3 + 1]); } } if (Math.Abs(t2.x - t1.x) > 0.5) { if (t1.x < 0.5) { indexToSplit.push_back(faces[i * 3 + 1]); } else { indexToSplit.push_back(faces[i * 3 + 2]); } } } //split vertices for (ushort i = 0; i < indexToSplit.size(); i++) { int index = indexToSplit[i]; //duplicate vertex Vector3 v = vertices[index]; Vector2 t = texCoords[index] + Vector2.UNIT_X; vertices.push_back(v); texCoords.push_back(t); int newIndex = vertices.size() - 1; //reassign indices for (ushort j = 0; j < faces.size(); j++) { if (faces[j] == index) { int index1 = faces[(j + 1) % 3 + (j / 3) * 3]; int index2 = faces[(j + 2) % 3 + (j / 3) * 3]; if ((texCoords[index1].x > 0.5f) || (texCoords[index2].x > 0.5f)) { faces[j] = newIndex; } } } } /// Step 5 : realize buffer.rebaseOffset(); buffer.estimateVertexCount((uint)vertices.size()); buffer.estimateIndexCount((uint)size); for (ushort i = 0; i < vertices.size(); i++) { addPoint(ref buffer, mRadius * vertices[i], vertices[i], new Vector2(texCoords[i].x, texCoords[i].y)); } for (ushort i = 0; i < size; i++) { buffer.index(offset + faces[i]); } offset += vertices.size(); }