needle.cpp 6.45 KB
Newer Older
pitiot's avatar
pitiot committed
1 2 3 4 5
#include "simulator.h"

// initialisation Needle
Needle::Needle(Simulator* sim,std::pair<VEC3, Dart> startingPoint)
    {
6 7

    closest_point=VEC3(1000,1000,1000);
pitiot's avatar
pitiot committed
8 9 10 11
    r=0.0f;
    g=0.0f;
    b=1.0f;
    sim_=sim;
12
    width=0.1;
pitiot's avatar
pitiot committed
13 14 15 16 17 18 19 20 21
    Needle_or_knife=true;
    nbVertices=1;
    nbEdges=0;
    edgeSize=1.0f;
    int maxEdges = 10000;
    distanceColorationMaxi=0.25f;
    distanceColorationMini=5.0f;
    belonging_cells = new std::vector<Dart>[maxEdges];
    neighbor_cells = new std::vector<Dart>[maxEdges];
22
     index_articulated=sim->objects.size();
pitiot's avatar
pitiot committed
23 24 25 26 27 28 29 30


        parts_.push_back(new CGoGN::Algo::Volume::MovingObjects::ParticleCell3D<PFP>(sim_->envMap_.map, startingPoint.second, startingPoint.first , sim_->envMap_.position));


        this->color_close();

        createEdge(0);
pitiot's avatar
pitiot committed
31
#ifdef IHMap
32
//        refine();
pitiot's avatar
pitiot committed
33
#endif
pitiot's avatar
pitiot committed
34 35
}

36

pitiot's avatar
pitiot committed
37 38 39 40 41
void Needle::color_close()
{
    if(nbVertices>0)
    {
        Dart volume = parts_[0]->d;
42 43
        VEC3 pos=parts_[0]->getPosition();
        float distSq=0;
pitiot's avatar
pitiot committed
44 45 46 47
        for (Segment * s : sim_->envMap_.RegisteredEdges[volume])
        {
            if(s->nid!=this || s->index!=nbEdges)
            {
48
                distSq = distSqPointLineSegment(s->p1,s->p2,pos);
49
                s->nid->changeColor(s,distSq);
pitiot's avatar
pitiot committed
50 51 52 53 54 55
            }
        }
        for (Segment * s : sim_->envMap_.RegisteredNeighborEdges[volume])
        {
            if(s->nid!=this || s->index!=nbEdges)
            {
56
                distSq = distSqPointLineSegment(s->p1,s->p2,pos);
57
                s->nid->changeColor(s,distSq);
pitiot's avatar
pitiot committed
58 59
            }
        }
60 61 62 63 64 65 66 67 68
        for(Triangle * t : sim_->envMap_.RegisteredTriangles[volume])
        {
            VEC3 closest = closestPointInTriangle(t->p1,t->p2,t->p3,pos);
//            CGoGNout<<"pos :"<<pos<<" || closest :"<<closest<<CGoGNendl;
            distSq = (pos-closest).norm2();
            if(distSq < ((pos-closest_point).norm2()))
                closest_point=closest;
            t->surf->changeColor(t,distSq);
        }
pitiot's avatar
pitiot committed
69 70 71 72 73 74 75 76 77 78
    }
}

void Needle::unColor_close(Dart volume)
{

    for (Segment * s : sim_->envMap_.RegisteredEdges[volume])
    {
        if(s->nid!=this || s->index!=nbEdges)
        {
79 80
             s->nid->changeColor(s,-1);

pitiot's avatar
pitiot committed
81 82 83 84 85 86
        }
    }
    for (Segment * s : sim_->envMap_.RegisteredNeighborEdges[volume])
    {
        if(s->nid!=this || s->index!=nbEdges)
        {
87
             s->nid->changeColor(s,-1);
pitiot's avatar
pitiot committed
88 89
        }
    }
90 91 92 93
    for(Triangle * t : sim_->envMap_.RegisteredTriangles[volume])
    {
        t->surf->changeColor(t,-1);
    }
pitiot's avatar
pitiot committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
}

void Needle::createEdge(int index)
{
    parts_.push_back(new CGoGN::Algo::Volume::MovingObjects::ParticleCell3D<PFP>(sim_->envMap_.map, parts_[0]->d, parts_[0]->getPosition() , sim_->envMap_.position));

    Segment* o = new Segment(parts_[index+1]->getPosition(),
                  parts_[0]->getPosition(),
                  this,index+1,0,index,r,g,b);
           Segments_.push_back(o);
           sim_->envMap_.FirstRegistrationSegment(o);
    if(index>0)
    {
        (Segments_[index-1])->indexPart2=index+1;
#ifdef DEBUG_affichage
             CGoGNout << "segment modifié : "<<index-1<<" nouvelle particule : "<< index+1 << CGoGNendl;
              CGoGNout << "etat particules  : "<< CGoGNendl;
              for(auto particule : parts_)
              {
                  CGoGNout << "particule (position, dart) : "<<particule->getPosition()<<" || "<< particule->d<< CGoGNendl;

              }

              CGoGNout << "etat segments  : "<< CGoGNendl;
              for(auto s : Segments_)
              {
                 CGoGNout << "segment (p1,p2,part1,part2, index)  : "<<s->p1<<" || "<<s->p2<<" || "<<s->indexPart1<<" || "<<s->indexPart2<<" || "<<s->index << CGoGNendl;

              }
#endif
    }


    nbEdges++;
    nbVertices++;

#ifdef DEBUG_affichage

            CGoGNout << "Nouvel edge (p1,p2,part1,part2)  : "<<o->p1<<" || "<<o->p2<<" || "<<o->indexPart1<<" || "<<o->indexPart2 << CGoGNendl;

#endif


}

void Needle::move(VEC3 diff)
{
//    VEC3 pos = getPosition(0)+diff;
//    parts_[0]->move(pos);
    Dart oldCell = parts_[0]->d;
    VEC3 pos = getPosition(0)+diff;
    if(sim_->envMap_.map.isVolumeIncidentToBoundary(parts_[0]->d))
    {
        if(!sim_->envMap_.checkPointInMap(pos,parts_[0]->d))
        {
            pos=pos-diff;
        }
    }

    parts_[0]->move(pos);
    updateRegistration();
    this->unColor_close(oldCell);

#ifdef IHMap
    if(!parts_[0]->crossCell==CGoGN::Algo::Volume::MovingObjects::NO_CROSS) /// a rajouter si les cellules sont assez petites
    {
        refine();
    }
#endif

     this->color_close();
        if((getPosition(0)-getPosition(nbEdges)).norm2()>edgeSize)
        {

            createEdge(nbEdges);
#ifdef DEBUG_affichage
            CGoGNout<<" init Segment :"<< nbEdges ;
            CGoGNout<<" || nb_segments  :"<< Segments_.size() <<CGoGNendl;
            CGoGNout<<CGoGNendl;
            CGoGNout<<CGoGNendl;

            for(unsigned int i =0;i<nbEdges;i++)
            {
                CGoGNout<<" Segment n° "<< i <<" enregistré dans "<< belonging_cells[i].size()<< " volumes." <<CGoGNendl;
            }
#endif
        }

}


void Needle::updateRegistration()
{
        Segment* o = Segments_[nbEdges-1];
        o->p1 = parts_[o->indexPart1]->getPosition();
        o->p2 = parts_[o->indexPart2]->getPosition();
        Dart d1 = parts_[o->indexPart1]->d;
        Dart d2 = parts_[o->indexPart2]->d;
        if(!((sim_->envMap_.map.sameVolume(d1,d2))&& (parts_[o->indexPart1]->crossCell==CGoGN::Algo::Volume::MovingObjects::NO_CROSS && parts_[o->indexPart2]->crossCell==CGoGN::Algo::Volume::MovingObjects::NO_CROSS)))
        {
#ifdef DEBUG_affichage
            CGoGNout<<"update registration "<<CGoGNendl<<CGoGNendl;
            CGoGNout << "p1 : "<<o->p1<<" || "<<d1 << CGoGNendl;
            CGoGNout << "p2 : "<<o->p2<<" || "<<d2 << CGoGNendl;
            CGoGNout<<CGoGNendl<<" fin update registration "<<CGoGNendl<<CGoGNendl;
#endif
            sim_->envMap_.popAndPushSegment(o);

        }



    /////affichage du general_belonging
//	CGoGNout<< CGoGNendl;
//	CGoGNout << "General : ";
//	for(std::vector<std::pair<Dart , int> >::iterator ite = general_belonging.begin();ite!=general_belonging.end(); ++ite)
//	{
//		CGoGNout<<"< "<<(*ite).first.index<<" , "<<(*ite).second<<"> ; ";
//	}
//	CGoGNout<< CGoGNendl;

}


void Needle::refine()
{
Thomas Pitiot 's avatar
Thomas Pitiot committed
220
#ifdef IHMap
pitiot's avatar
pitiot committed
221
    Dart volume = parts_[0]->d;
Thomas Pitiot 's avatar
Thomas Pitiot committed
222

pitiot's avatar
pitiot committed
223 224
    REAL rayon = sim_->envMap_.volumeMaxdistance(volume) ;
    if(rayon > 2.0f )
pitiot's avatar
pitiot committed
225
    {
pitiot's avatar
pitiot committed
226
        if(sim_->envMap_.subdivideVolume(volume))
227
        {
Thomas Pitiot 's avatar
Thomas Pitiot committed
228
            refine();
229
        }
pitiot's avatar
pitiot committed
230 231
    }

Thomas Pitiot 's avatar
Thomas Pitiot committed
232
#endif
pitiot's avatar
pitiot committed
233 234

}