Exemple #1
0
int R3Mesh::
Read(const char *filename)
{
  // Parse input filename extension
  const char *extension;
  if (!(extension = strrchr(filename, '.'))) {
    printf("Filename %s has no extension (e.g., .ply)\n", filename);
    return 0;
  }

  // Read file of appropriate type
  int status = 0;
  if (!strncmp(extension, ".ray", 4)) 
    status = ReadRay(filename);
  else if (!strncmp(extension, ".off", 4)) 
    status = ReadOff(filename);
  else if (!strncmp(extension, ".jpg", 4)) 
    status = ReadImage(filename);
  else if (!strncmp(extension, ".jpeg", 4)) 
    status = ReadImage(filename);
  else if (!strncmp(extension, ".bmp", 4)) 
    status = ReadImage(filename);
  else if (!strncmp(extension, ".ppm", 4)) 
    status = ReadImage(filename);
  else {
    fprintf(stderr, "Unable to read file %s (unrecognized extension: %s)\n", filename, extension);
    return status;
  }

  // Update mesh data structures
  Update();

  // Return success
  return 1;
}
Exemple #2
0
 string countAndSay(int n) {
     string ans = "1";
     if (n == 1) return ans;
     while (n > 1) {
         ans = ReadOff(ans);
         n--;
     }
     return ans;
 }
Exemple #3
0
Mesh::Mesh(const string& file, int camNum/*=0*/)
{
#ifndef _EXEC_FAST
	printf("%s%s","Reading ",file.c_str());
#endif

	ifstream obj(file.c_str());

	if(!obj.is_open()) { printf("can not read the mesh file!\n"); return;}

	if(file.length() < 4)
		return;

	if(string(file.end() - 4, file.end()) == string(".off"))
		ReadOff(obj,camNum);     
	else if(string(file.end() - 5, file.end()) == string(".mesh"))
		ReadMesh(obj, camNum);
	else if(string(file.end() - 6, file.end()) == string(".dmesh"))
		ReadMesh(obj,camNum);
	else
		return;

	obj.close();

	int verts = int(vertices.size());
	if(verts == 0)
		return;

	for(int i = 0; i < (int)edges.size(); ++i)					//保证所有的顶点都是有效的
	{ 
		if(edges.at(i).vertex<0||edges.at(i).vertex>=verts)
		{
			vertices.clear(); 
			edges.clear(); 
			cout<<"error!"<<i<<endl;
			return;
		}
	}

	computeVertexNormals();
#ifndef _EXEC_FAST
	printf(" success!\n");
#endif
}
int  main()
{
    //Specify data directory
    //const std::string dataDirectory = "/Users/ervislilaj/Desktop/data/SimudPuteh";
    const std::string dataDirectory = "/Users/ervislilaj/Desktop/data/SmallCaveDownsampled";
    
    //Calculate paths
    const std::string outputDirectory = dataDirectory + "/output";
    
    const std::string offFile = dataDirectory + "/model.off";
    const std::string skeletonFile = dataDirectory + "/model.skel";
    
    const std::string segmentationFile = dataDirectory + "/segmentation.seg";
    const std::string segmentationFile2 = dataDirectory + "/segmentation2.seg";
    
    
    
    //Read files
    typedef std::vector<Triangle> TriangleList;
    std::vector<Eigen::Vector3f> vertices;
    std::vector<Eigen::Vector3f> newVertices /*(vertices.size()*2)*/;
    TriangleList triangles;
    std::vector<Eigen::Vector3f> border;
    std::vector<CurveSkeleton::Vertex> skeletonVertices;
    
    
    std::vector<Vertex> gang;
    std::vector<Eigen::Vector3f> borderVertices;
    std::vector<Eigen::Vector3f> intersectionVertexForTriangulation;
    
    std::vector<IndexedTriangle> triIndices;
    std::vector<IndexedTriangle> borderIndices;
    std::vector<IndexedTriangle> labelIndices;
    std::vector<IndexedTriangle> leerIndices;
    
    std::vector<Eigen::Vector3f> verwe;
    
    std::vector<int32_t> segmentation;
    std::vector<int32_t> segmentation2;
    std::vector<double> doubleSeg;
    std::vector<int> labels;
    
    
    //step & iteration parameter
    int iter = 490;
    double step = 0.001;

    std::cout << "Loading mesh file..." << std::endl;
    ReadOff(offFile, vertices, triangles, triIndices);
    
    std::cout << "Loading skeleton..." << std::endl;
    CurveSkeleton* skeleton = LoadCurveSkeleton(skeletonFile.c_str());
    
    std::cout << "Loading segmentation..." << std::endl;
    ReadSegmentation(segmentationFile2, segmentation2, skeleton);
    //copy
    
    std::vector<Vertex> vec2;
    
    std::cout << "Calculating inverse correspondences..." << std::endl;
    //Calculate inverse correspondences
    std::vector<unsigned int> meshVertexCorrespondsTo(vertices.size());
    int iVert = -1;
    for (auto& vert : skeleton->vertices)
    {
        ++iVert;
        for (auto c : vert.correspondingOriginalVertices)
        {
            meshVertexCorrespondsTo[c] = iVert;
        }
        
    }
    
    for (int i = 0; i < segmentation2.size(); ++i) {
        doubleSeg.push_back(segmentation2[i]);
    }
    
    for (int i = 0; i < vertices.size(); ++i) {
        Vertex* new_v = new Vertex();
        new_v->p = vertices[i];
        new_v->c = doubleSeg[meshVertexCorrespondsTo[i]] + 0.5;
        newVertices.push_back(vertices[i]);
        vec2.push_back(new_v[0]);
    }
    
    
    cgv::math::union_find ufTri((int)vertices.size());
    
    borderIndices.clear();
    labels.clear();
    labelIndices.clear();
    
    
    //set neighbors id
    for (int i = 0; i < triIndices.size(); ++i)
    {
        for (int j = 0; j < triIndices.size(); ++j)
        {
            if(i==j);
            else{
                if (((triIndices[j].i[0] == triIndices[i].i[0]) && (triIndices[j].i[1] == triIndices[i].i[1])) ||
                    ((triIndices[j].i[1] == triIndices[i].i[0]) && (triIndices[j].i[0] == triIndices[i].i[1])) ||
                    
                    ((triIndices[j].i[1] == triIndices[i].i[0]) && (triIndices[j].i[2] == triIndices[i].i[1])) ||
                    ((triIndices[j].i[2] == triIndices[i].i[0]) && (triIndices[j].i[1] == triIndices[i].i[1])) ||
                    
                    ((triIndices[j].i[2] == triIndices[i].i[0]) && (triIndices[j].i[0] == triIndices[i].i[1])) ||
                    ((triIndices[j].i[0] == triIndices[i].i[0]) && (triIndices[j].i[2] == triIndices[i].i[1])))
                {
                    //  neighbor 1
                    triIndices[i].n[0] = &triIndices[j];
                }
                
                if  (((triIndices[j].i[0] == triIndices[i].i[1]) && (triIndices[j].i[1] == triIndices[i].i[2])) ||
                     ((triIndices[j].i[1] == triIndices[i].i[1]) && (triIndices[j].i[0] == triIndices[i].i[2])) ||
                     
                     ((triIndices[j].i[1] == triIndices[i].i[1]) && (triIndices[j].i[2] == triIndices[i].i[2])) ||
                     ((triIndices[j].i[2] == triIndices[i].i[1]) && (triIndices[j].i[1] == triIndices[i].i[2])) ||
                     
                     ((triIndices[j].i[2] == triIndices[i].i[1]) && (triIndices[j].i[0] == triIndices[i].i[2])) ||
                     ((triIndices[j].i[0] == triIndices[i].i[1]) && (triIndices[j].i[2] == triIndices[i].i[2])))
                {
                    //  neighbor 2
                    triIndices[i].n[1] = &triIndices[j];
                }
                
                if  (((triIndices[j].i[0] == triIndices[i].i[2]) && (triIndices[j].i[1] == triIndices[i].i[0])) ||
                     ((triIndices[j].i[1] == triIndices[i].i[2]) && (triIndices[j].i[0] == triIndices[i].i[0])) ||
                     
                     ((triIndices[j].i[1] == triIndices[i].i[2]) && (triIndices[j].i[2] == triIndices[i].i[0])) ||
                     ((triIndices[j].i[2] == triIndices[i].i[2]) && (triIndices[j].i[1] == triIndices[i].i[0])) ||
                     
                     ((triIndices[j].i[2] == triIndices[i].i[2]) && (triIndices[j].i[0] == triIndices[i].i[0])) ||
                     ((triIndices[j].i[0] == triIndices[i].i[2]) && (triIndices[j].i[2] == triIndices[i].i[0])))
                {
                    //  neighbor 3
                    triIndices[i].n[2] = &triIndices[j];
                }
            }
        }
    }
    
    /*
     std::vector<IndexedTriangle> toShow;
     for(int i = 0; i < triIndices.size(); i += 50)
     {
     toShow.push_back(triIndices[i]);
     toShow.push_back(*triIndices[i].n[0]);
     toShow.push_back(*triIndices[i].n[1]);
     toShow.push_back(*triIndices[i].n[2]);
     }
     */
    
    
    borderIndices.clear();
    labels.clear();
    labelIndices.clear();
    
    
    for (int i = 0; i < vec2.size(); ++i) //für jeden Vertex
    {
        for (int j = 0; j < triIndices.size(); ++j) //für jedes Dreieck
        {
            if ((i == triIndices[j].i[0] || i == triIndices[j].i[1] || i == triIndices[j].i[2]))
            {
                vec2[i].NeigborTri.push_back(j);
            }
            
        }
    }
    
    
    int nrOptimization = 0;
    double tmpLengthIter = 0;
    double lengthIter = 0;
    bool firstTimeIter = false;
    
    do
    {
        std::ofstream fout("iter/50iter" + std::to_string(nrOptimization) + ".xyz", std::ofstream::out);
        
        if(!firstTimeIter)
            tmpLengthIter = 99999999;
        
        tmpLengthIter = lengthIter;
        lengthIter = 0;
        
        
        // 1 richtung
        for (int i = 0; i < vec2.size(); ++i) //für jeden Vertex
        {
            double inzidenzEdgesLength = 0;
            double tmp = 0;
            int index = 0;
            do {
                
                Edge e;
                tmp = inzidenzEdgesLength;
                if (index == 0)
                    tmp = 10000;
                inzidenzEdgesLength = 0;
                
                for (int j = 0; j < vec2[i].NeigborTri.size(); ++j) //für jedes Dreieck
                {
                    
                    triIndices[vec2[i].NeigborTri[j]].visible = true;
                    if ((i == triIndices[vec2[i].NeigborTri[j]].i[0] || i == triIndices[vec2[i].NeigborTri[j]].i[1] || i == triIndices[vec2[i].NeigborTri[j]].i[2]))
                    {
                        //Kanten rechnen sowie deren länge
                        if (ComputePointsBetweenEdges_v1(vec2, triIndices[vec2[i].NeigborTri[j]], e))
                        {
                            e.calculateLength();
                            inzidenzEdgesLength += e.length;
                        }
                    }
                    
                }
                
                index++;
                
                vec2[i].c += step;
                
                
                
                
            } while (tmp > inzidenzEdgesLength );
            
            
            vec2[i].c -= step;
            vec2[i].edgeLength = inzidenzEdgesLength;
        }
        
        // 2 richtung
        for (int i = 0; i < vec2.size(); ++i) //für jeden Vertex
        {
            double inzidenzEdgesLength = 0;
            double tmp = 0;
            int index = 0;
            do {
                
                Edge e;
                tmp = inzidenzEdgesLength;
                if (index == 0)
                    tmp = vec2[i].edgeLength;
                inzidenzEdgesLength = 0;
                
                for (int j = 0; j < vec2[i].NeigborTri.size(); ++j) //für jedes Dreieck
                {
                    
                    triIndices[vec2[i].NeigborTri[j]].visible = true;
                    if ((i == triIndices[vec2[i].NeigborTri[j]].i[0] || i == triIndices[vec2[i].NeigborTri[j]].i[1] || i == triIndices[vec2[i].NeigborTri[j]].i[2]))
                    {
                        //Kanten rechnen sowie deren länge
                        if (ComputePointsBetweenEdges_v1(vec2, triIndices[vec2[i].NeigborTri[j]], e))
                        {
                            e.calculateLength();
                            inzidenzEdgesLength += e.length;
                        }
                    }
                    
                }
                //edgeLengthBlock = inzidenzEdgesLength
                index++;
                
                vec2[i].c -= step;
                
                
            lengthIter += inzidenzEdgesLength;
            } while (tmp > inzidenzEdgesLength );
            
            vec2[i].c += step;
            
        }
        
        for (int j = 0; j < triIndices.size(); ++j) //für jedes Dreieck
        {
            Edge e2;
            if (ComputePointsBetweenEdges_v1(vec2, triIndices[j], e2)) {
                fout << e2.a.x() << " " << e2.a.y() << " " << e2.a.z() << std::endl;
                fout << e2.b.x() << " " << e2.b.y() << " " << e2.b.z() << std::endl;
            }
        }
        
        nrOptimization++;
        fout.close();
        std::cout << nrOptimization << std::endl;
        std::cout << lengthIter << std::endl;
        
        
    } while ( (nrOptimization < iter ) && ((tmpLengthIter >= lengthIter) || (nrOptimization < 450)) );
    
    /*End Optimization*/
    
    
    
    
    borderVertices.clear();
    for (int j = 0; j < triIndices.size(); ++j) //für jedes Dreieck
    {
        triIndices[j].iD = j;
        Edge e2;
        if (ComputePointsBetweenEdges_v1(vec2, triIndices[j], e2)) {
            borderVertices.push_back(e2.a);
            borderVertices.push_back(e2.b);
        }
    }
    
    // übergang erkennen
    for (int i = 0; i < triIndices.size(); ++i) {
        
        
        auto v0 = vec2.begin() + (triIndices[i].i[0]);
        auto v1 = vec2.begin() + (triIndices[i].i[1]);
        auto v2 = vec2.begin() + (triIndices[i].i[2]);
        
        double first = v0[0].c;
        double second = v1[0].c;
        double third = v2[0].c;
        
        if ((first >= 0.f && second < 0.f) || (first < 0.f && second >= 0.f) || (third >= 0.f && second < 0.f) || (third < 0.f && second >= 0.f) || (third >= 0.f && first < 0.f) || (third < 0.f && first >= 0.f)) {
            
            IndexedTriangle t;
            
            t.i[0] = triIndices[i].i[0];
            t.i[1] = triIndices[i].i[1];
            t.i[2] = triIndices[i].i[2];
            t.iD = triIndices[i].iD;
            t.n[0] = triIndices[i].n[0];
            t.n[1] = triIndices[i].n[1];
            t.n[2] = triIndices[i].n[2];
            borderIndices.push_back(t);
        }
        
    }
    
    //schallen unite
    for (int i = 0; i < borderIndices.size(); ++i)
        for (int j = 0; j < borderIndices.size(); ++j)
        {
            if ((borderIndices[i].i[0] == borderIndices[j].i[0]) || (borderIndices[i].i[0] == borderIndices[j].i[1]) || (borderIndices[i].i[0] == borderIndices[j].i[2]))
            {
                ufTri.unite(i, j);
            }
            
            if ((borderIndices[i].i[1] == borderIndices[j].i[0]) || (borderIndices[i].i[1] == borderIndices[j].i[1]) || (borderIndices[i].i[1] == borderIndices[j].i[2]))
            {
                ufTri.unite(i, j);
            }
            
            if ((borderIndices[i].i[2] == borderIndices[j].i[0]) || (borderIndices[i].i[2] == borderIndices[j].i[1]) || (borderIndices[i].i[2] == borderIndices[j].i[2]))
            {
                ufTri.unite(i, j);
            }
        }
    std::map<int, std::vector<IndexedTriangle>> labelmain;
    
    // in LabelIndices 1 eiziger gang speichern
    for (int i = 0; i < borderIndices.size(); ++i)
    {
        if (ufTri.num_in_set(ufTri.find(i)) <= 1);
        else {
            
            if (std::find(labels.begin(), labels.end(), ufTri.find(i)) != labels.end());
            else {
                labels.push_back(ufTri.find(i));
            }
        }
        
        if (labels.size() != 0)
        {
            int key = ufTri.find(i);
            
            labelmain[key].push_back(borderIndices[i]);
            
        }
    }
    std::vector<IndexedTriangle> newTriangles;
    
    
    
    int offset = (int)vec2.size();
    for (int label : labels)
        
    {
        labelIndices = labelmain[label];
        
        
        std::vector<Eigen::Vector3f> labelVertices;
        std::vector<Eigen::Vector3f> halleVertices;
        
        labelVertices.clear();
        Edge labelEdge;
        
        
        for (int i = 0; i < labelIndices.size(); ++i)
        {
            if (ComputePointsBetweenEdges_v1(vec2, labelIndices[i], labelEdge))
            {
                labelVertices.push_back(labelEdge.a);
                labelVertices.push_back(labelEdge.b);
            }
        }
        
        /*Plane Fitting with SVD */
        Eigen::Vector3f vor_c(0, 0, 0);
        Eigen::Vector3f c(0, 0, 0);
        Eigen::MatrixXf matA(3, labelVertices.size());
        
        // calculate c
        for (int i = 0; i < labelVertices.size(); ++i)
        {
            vor_c += labelVertices[i];
        }
        
        c = vor_c / labelVertices.size();
        
        //fill the Matrix
        for (int i = 0; i < labelVertices.size(); i++)
        {
            float xB, yB, zB;
            xB = labelVertices[i].x() - c.x();
            yB = labelVertices[i].y() - c.y();
            zB = labelVertices[i].z() - c.z();
            matA.col(i) << xB, yB, zB;
        }
        
        Eigen::JacobiSVD<Eigen::MatrixXf> svd(matA, Eigen::ComputeThinU | Eigen::ComputeThinV);
        Eigen::Vector3f planeNormal(0, 0, 0);
        planeNormal = svd.matrixU().col(2);
        float d = -(planeNormal.x()*c.x() + planeNormal.y()*c.y() + planeNormal.z()*c.z());
        
        /*Begin Normal direction of the plane*/
        std::vector<Eigen::Vector3f> halleVerticesSidePLane;
        std::vector<double> disthalleVerticesSidePLane;
        int indexHalleV;
       
        Eigen::Vector3f halle_cVector;
        
        
        //alle halle knoten hinzufügen
        for(int i = 0; i < labelIndices.size(); ++i)
        {
            if(vec2[labelIndices[i].i[0]].c >= 0)
                halleVerticesSidePLane.push_back(vec2[labelIndices[i].i[0]].p);
            
            if(vec2[labelIndices[i].i[1]].c >= 0)
                halleVerticesSidePLane.push_back(vec2[labelIndices[i].i[1]].p);
            
            if(vec2[labelIndices[i].i[2]].c >= 0)
                halleVerticesSidePLane.push_back(vec2[labelIndices[i].i[2]].p);
            
        }
        
        for(int i = 0; i < halleVerticesSidePLane.size(); ++i)
        {
            disthalleVerticesSidePLane.push_back( Dist2Plane(halleVerticesSidePLane[i], planeNormal, d));
        }
        
        indexHalleV = *std::max_element(disthalleVerticesSidePLane.begin(), disthalleVerticesSidePLane.end());
        
        halle_cVector = halleVerticesSidePLane[indexHalleV] - c;
        
        Eigen::Vector3f diffBetweenPlane_halle_positive = halle_cVector + planeNormal;
        Eigen::Vector3f diffBetweenPlane_halle_negative = halle_cVector - planeNormal;
        
        float lengthPositive =sqrt(diffBetweenPlane_halle_positive.x()* diffBetweenPlane_halle_positive.x() + diffBetweenPlane_halle_positive.y()* diffBetweenPlane_halle_positive.y() + diffBetweenPlane_halle_positive.z()* diffBetweenPlane_halle_positive.z() );
        float lengthNegative =sqrt(diffBetweenPlane_halle_negative.x()* diffBetweenPlane_halle_negative.x() + diffBetweenPlane_halle_negative.y()* diffBetweenPlane_halle_negative.y() + diffBetweenPlane_halle_negative.z()* diffBetweenPlane_halle_negative.z() );
        
        if (lengthPositive > lengthNegative)
            planeNormal = -planeNormal;
        else if(lengthPositive <= lengthNegative)
            planeNormal = planeNormal;
        
        
        
        // ax + bx + cx + d = 0 Plane equation
        d = -(planeNormal.x()*c.x() + planeNormal.y()*c.y() + planeNormal.z()*c.z());
        

        
        /*Begin computing Intersection Points*/
        
        
        
        
        //Point which has the largest distance to c
        std::vector<double> MaxDistanceToC;
        double radiusMax;
        
        for (int i = 0; i < labelVertices.size(); ++i)
        {
            MaxDistanceToC.push_back(Dist2Points(c, labelVertices[i]));
        }
        
        radiusMax = *std::max_element(MaxDistanceToC.begin(), MaxDistanceToC.end());
        
        //Computing Intersection points
        
        int succesor = offset - 1;
        
        std::vector<Edge> intersectionEdges;
        intersectionEdges.clear();
        
   
        //Label Nachbarn hinzufügen. 8schritte  3^12

        
        // First Triangle Hinzufügen die geschnitten wird und teil von lableindices ist
        IndexedTriangle firstTri;
        int firstTriID = 0;
        bool firstTimeSearch = true;
        
        for (int i = 0; i < labelIndices.size(); ++i){
            Eigen::Vector3f aP, bP, cP, intersection1, intersection2;
         
            int aI = 0, bI = 0, cI = 0;
           
            aI = labelIndices[i].i[0];
            bI = labelIndices[i].i[1];
            cI = labelIndices[i].i[2];
            
            aP = vec2[aI].p;
            bP = vec2[bI].p;
            cP = vec2[cI].p;
            
            if((SegPlaneIntersection(aP, bP, intersection1, planeNormal, d) || SegPlaneIntersection(aP, cP, intersection2, planeNormal, d) ||
                SegPlaneIntersection(bP, cP, intersection2, planeNormal, d)) && firstTimeSearch)
            {
                firstTri = triIndices[labelIndices[i].iD];
                firstTimeSearch = false;
                firstTriID = labelIndices[i].iD;
            }
                
                
        }
        
        std::set<int> triIndicesHit ;
        int mico = 0;
        do{
            for (int m = 0; m < 3 ; ++m)
            {
                Eigen::Vector3f aP, bP, cP, intersection1, intersection2;
                IndexedTriangle t1,t2;
                //indices
               int aI = firstTri.n[m]->i[0];
               int bI = firstTri.n[m]->i[1];
               int cI = firstTri.n[m]->i[2];
                
                
                aP = vec2[aI].p;
                bP = vec2[bI].p;
                cP = vec2[cI].p;
                
                
            if((SegPlaneIntersection(aP, bP, intersection1, planeNormal, d) || SegPlaneIntersection(aP, cP, intersection2, planeNormal, d) ||
                SegPlaneIntersection(bP, cP, intersection2, planeNormal, d)) && (triIndicesHit.find(firstTri.n[m]->iD) == triIndicesHit.end())){
                
                triIndicesHit.insert(firstTri.n[m]->iD);
               
                if (SegPlaneIntersection(aP, bP, intersection1, planeNormal, d) && SegPlaneIntersection(aP, cP, intersection2, planeNormal, d) && (Dist2Plane(aP, planeNormal, d) < 0))
                {
                    
                    newVertices.push_back(intersection1);
                    newVertices.push_back(intersection2);
                    
                    t1.i[0] = aI;
                    t1.i[2] = succesor + 2;
                    t1.i[1] = succesor + 1;
                    
                     t1.visible = true;
                    
                    newTriangles.push_back(t1);
             
                    firstTri = *firstTri.n[m];
                    
                    succesor++;
                    succesor++;
                }
                if (SegPlaneIntersection(bP, cP, intersection1, planeNormal, d) && SegPlaneIntersection(bP, aP, intersection2, planeNormal, d) && (Dist2Plane(bP, planeNormal, d) < 0))
                {
                    newVertices.push_back(intersection1);
                    newVertices.push_back(intersection2);
                    
                    t1.i[0] = bI;
                    t1.i[2] = succesor + 2;
                    t1.i[1] = succesor + 1;
                    
                     t1.visible = true;
                    
                    newTriangles.push_back(t1);
                    
            
                    firstTri = *firstTri.n[m];
                    succesor++;
                    succesor++;
                }
                if (SegPlaneIntersection(cP, aP, intersection1, planeNormal, d) && SegPlaneIntersection(cP, bP, intersection2, planeNormal, d) && (Dist2Plane(cP, planeNormal, d) < 0))
                {
                    newVertices.push_back(intersection1);
                    newVertices.push_back(intersection2);
                    
                    t1.i[0] = cI;
                    t1.i[2] = succesor + 2;
                    t1.i[1] = succesor + 1;
                   
                    t1.visible = true;
                   
                    newTriangles.push_back(t1);
                 
                    firstTri = *firstTri.n[m];
                    
                    succesor++;
                    succesor++;
                    
                }
                
                
                if (SegPlaneIntersection(aP, bP, intersection1, planeNormal, d) && SegPlaneIntersection(aP, cP, intersection2, planeNormal, d) && (Dist2Plane(bP, planeNormal, d) < 0) && (Dist2Plane(cP, planeNormal, d) < 0))
                {
                    newVertices.push_back(intersection1);
                    newVertices.push_back(intersection2);
                    
                    t1.i[0] = bI;
                    t1.i[2] = succesor + 1;
                    t1.i[1] = succesor + 2;
                    
                    t2.i[0] = cI;
                    t2.i[2] = bI;
                    t2.i[1] = succesor + 2;
                    t1.visible = true;
                    t2.visible = true;
                    
                    newTriangles.push_back(t1);
                    newTriangles.push_back(t2);
                    
                    firstTri = *firstTri.n[m];
                    
                    succesor++;
                    succesor++;
                }
                if (SegPlaneIntersection(bP, cP, intersection1, planeNormal, d) && SegPlaneIntersection(bP, aP, intersection2, planeNormal, d) && (Dist2Plane(cP, planeNormal, d) < 0) && (Dist2Plane(aP, planeNormal, d) < 0))
                {
                    newVertices.push_back(intersection1);
                    newVertices.push_back(intersection2);
                    
                    t1.i[0] = cI;
                    t1.i[2] = succesor + 1;
                    t1.i[1] = succesor + 2;
                    
                    t2.i[0] = aI;
                    t2.i[2] = cI;
                    t2.i[1] = succesor + 2;
                    
                    t1.visible = true;
                    t2.visible = true;
                    
                    newTriangles.push_back(t1);
                    newTriangles.push_back(t2);
        
                    firstTri = *firstTri.n[m];
                    
                    succesor++;
                    succesor++;
                }
                if (SegPlaneIntersection(cP, aP, intersection1, planeNormal, d) && SegPlaneIntersection(cP, bP, intersection2, planeNormal, d) && (Dist2Plane(aP, planeNormal, d) < 0) && (Dist2Plane(bP, planeNormal, d) < 0))
                {
                    newVertices.push_back(intersection1);
                    newVertices.push_back(intersection2);
                    
                    t1.i[0] = aI;
                    t1.i[2] = succesor + 1;
                    t1.i[1] = succesor + 2;
                    
                    t2.i[0] = bI;
                    t2.i[2] = aI;
                    t2.i[1] = succesor + 2;
                    
                    t1.visible = true;
                    t2.visible = true;
                    
                    newTriangles.push_back(t1);
                    newTriangles.push_back(t2);
                    
                    firstTri = *firstTri.n[m];
                    
                    succesor++;
                    succesor++;
                }
                
            }
                
        }
            
            mico ++;
            
        }while (mico < 1000000);
        
        
        //nachbarn von Schnitt hinzufügen
        std::set<int> extendedLabelIndices ;
        for (int i = 0; i < triIndices.size(); ++i)
        {
            const bool is_in = triIndicesHit.find(triIndices[i].iD) != triIndicesHit.end();
            
            if(is_in){
            extendedLabelIndices.insert(triIndices[i].iD);
                for(int j = 0; j < 3; ++j){
                    extendedLabelIndices.insert(triIndices[i].n[j]->iD);
                    for(int k = 0; k < 3; ++k){
                        extendedLabelIndices.insert(triIndices[i].n[j]->n[k]->iD);
                        for(int l = 0; l < 3; ++l){
                            extendedLabelIndices.insert(triIndices[i].n[j]->n[k]->n[l]->iD);
                            for(int f = 0; f < 3; ++f){
                                extendedLabelIndices.insert(triIndices[i].n[j]->n[k]->n[l]->n[f]->iD);
                                for(int s = 0; s < 3; ++s){
                                    extendedLabelIndices.insert(triIndices[i].n[j]->n[k]->n[l]->n[f]->n[s]->iD);
                                    for(int r = 0; r < 3; ++r){
                                        extendedLabelIndices.insert(triIndices[i].n[j]->n[k]->n[l]->n[f]->n[s]->n[r]->iD);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        
     /*   for (int i = 0; i < triIndices.size(); ++i)
        {
            int a = triIndices[i].i[0],b = triIndices[i].i[1] ,c = triIndices[i].i[2] ;
            if(( vec2[a].c >= 0 )&& ( vec2[b].c >= 0 ) && ( vec2[c].c >= 0 ))
            {
                triIndices[i].visible = true;
            }
            
        }*/
        
        
        for (int i = 0; i < triIndices.size(); ++i)
        {
            //positions
            Eigen::Vector3f aP, bP, cP, intersection1, intersection2;
            //distance to middlepoint
            float dista, distb, distc;
            //indices
            int aI = 0, bI = 0, cI = 0;
            
            
            aI = triIndices[i].i[0];
            bI = triIndices[i].i[1];
            cI = triIndices[i].i[2];
            
            aP = vec2[aI].p;
            bP = vec2[bI].p;
            cP = vec2[cI].p;
            
            dista = Dist2Points(c, aP);
            distb = Dist2Points(c, bP);
            distc = Dist2Points(c, cP);
            
            const bool is_in = extendedLabelIndices.find(triIndices[i].iD) != extendedLabelIndices.end();
            
            
            
            if (is_in)
            {
                if ((Dist2Plane(aP, planeNormal, d) > 0) || (Dist2Plane(bP, planeNormal, d) > 0) || (Dist2Plane(cP, planeNormal, d) > 0))
                {
                    triIndices[i].visible = false;
                }
                if (((Dist2Plane(aP, planeNormal, d) <= 0) || (Dist2Plane(bP, planeNormal, d) <= 0) || (Dist2Plane(cP, planeNormal, d) <= 0)) && ((Dist2Plane(aP, planeNormal, d) > 10) || (Dist2Plane(bP, planeNormal, d) > 10) || (Dist2Plane(cP, planeNormal, d) > 10)))
                {
                    triIndices[i].visible = true;
                }
            }
            
        }
        
        
        
        
        
        //umbrella
        Eigen::Vector3f vor_ca(0,0,0), ca(0, 0, 0);
        int anz = (int)newVertices.size() - (offset);
        for (int j =  offset ; j < (int)newVertices.size() ; ++j)
        {
            vor_ca += newVertices[j];
        }
        
        ca = vor_c / anz;
        newVertices.push_back(c);
        
        for (int j =  offset; j < newVertices.size()-2; j+=2)
        {
            IndexedTriangle t;
            t.i[1] = j;
            t.i[2] = j + 1;
            t.i[0] = (int)newVertices.size() - 1;
            newTriangles.push_back(t);
        }
        offset = (int)newVertices.size();
        /*End computing Intersection Points*/
    }
    for(auto t : triIndices)
        if(t.visible && (vec2[t.i[0]].c >= 0 || vec2[t.i[1]].c >= 0 || vec2[t.i[2]].c >= 0))
            newTriangles.push_back(t);
    
    
    
    
    
    
    WriteSegmentation(segmentationFile, segmentation2);
    ReadSegmentation(segmentationFile, segmentation, skeleton);
    
    
    //Test output
    std::cout << "Writing output file..." << std::endl;
    /*int colors[10][3] =
     {
     { 166, 206, 227 },
     { 31, 120, 180 },
     { 251, 154, 153 },
     { 227, 26, 28 },
     { 253, 191, 111 },
     { 255, 127, 0 },
     { 202, 178, 214 },
     { 106, 61, 154 },
     { 255, 255, 153 },
     { 177, 89, 40 }
     };*/
    auto colorFunc = [&](int i, int& r, int& g, int& b)
    {
        
        
        if (doubleSeg[i] == -1)
        {
            //color for passages
            r = 0; g = 175; b = 0;
        }
        else
            if (doubleSeg[i] >= 0)
            {
                r = 166; g = 175; b = 227;
            }
            else
                if (doubleSeg[i] == 0)
                {
                    r = 0; g = 0; b = 175;
                }
        
        
    };
    
    
    
    std::string segmentedMeshFile = outputDirectory + "/segmentedMesh.off";
    WriteOff(segmentedMeshFile.c_str(), vertices, triIndices, [&](int i, int& r, int& g, int& b) {colorFunc(meshVertexCorrespondsTo[i], r, g, b); });
    
    
    std::string segmentedMeshFile2 = outputDirectory + "/borderVertices.off";
    WriteOff(segmentedMeshFile2.c_str(), vertices, borderIndices, [&](int i, int& r, int& g, int& b) {colorFunc(meshVertexCorrespondsTo[i], r, g, b); });
    
    
    std::string segmentedMeshFile3 = outputDirectory + "/schalle.off";
    WriteOff(segmentedMeshFile3.c_str(), vertices, borderIndices, [&](int i, int& r, int& g, int& b) {colorFunc(meshVertexCorrespondsTo[i], r, g, b); });
    
    
    std::string segmentedMeshFile7 = outputDirectory + "/marchingBig.off";
    WriteOff(segmentedMeshFile7.c_str(), verwe , leerIndices, [&](int i, int& r, int& g, int& b) { r = 175; g = 0; b = 0; });
    
    std::string segmentedMeshFile8 = outputDirectory + "/new.off";
    WriteOff(segmentedMeshFile8.c_str(), newVertices, newTriangles, [&](int i, int& r, int& g, int& b) { r = 200; g = 200; b = 0; });
    
    //system("PAUSE");
}