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

// initialisation Needle
Needle::Needle(Simulator* sim,std::pair<VEC3, Dart> startingPoint)
    {
Thomas Pitiot 's avatar
Thomas Pitiot committed
6 7

    closest_point=VEC3(1000,1000,1000);
pitiot's avatar
up  
pitiot committed
8 9 10 11
    r=0.0f;
    g=0.0f;
    b=1.0f;
    sim_=sim;
12
    width=0.1;
pitiot's avatar
up  
pitiot committed
13 14 15 16 17 18 19
    Needle_or_knife=true;
    nbVertices=1;
    nbEdges=0;
    edgeSize=1.0f;
    int maxEdges = 10000;
    distanceColorationMaxi=0.25f;
    distanceColorationMini=5.0f;
Etienne Schmitt's avatar
Etienne Schmitt committed
20 21
    belonging_cells.resize(maxEdges);
    neighbor_cells.resize(maxEdges);
22
     index_articulated=sim->objects.size();
pitiot's avatar
up  
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
up  
pitiot committed
31
#ifdef IHMap
32
        refine();
pitiot's avatar
up  
pitiot committed
33
#endif
pitiot's avatar
up  
pitiot committed
34 35
}

36

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

void Needle::unColor_close(Dart volume)
{

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

pitiot's avatar
up  
pitiot committed
92 93 94 95 96 97
        }
    }
    for (Segment * s : sim_->envMap_.RegisteredNeighborEdges[volume])
    {
        if(s->nid!=this || s->index!=nbEdges)
        {
98
             s->nid->changeColor(s,-1);
pitiot's avatar
up  
pitiot committed
99 100
        }
    }
Thomas Pitiot 's avatar
Thomas Pitiot committed
101 102 103 104
    for(Triangle * t : sim_->envMap_.RegisteredTriangles[volume])
    {
        t->surf->changeColor(t,-1);
    }
Thomas Pitiot 's avatar
Thomas Pitiot committed
105 106 107 108
    for(Triangle * t : sim_->envMap_.RegisteredNeighborTriangles[volume])
    {
        t->surf->changeColor(t,-1);
    }
pitiot's avatar
up  
pitiot committed
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
}

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
235
#ifdef IHMap
pitiot's avatar
up  
pitiot committed
236
    Dart volume = parts_[0]->d;
Thomas Pitiot 's avatar
merged  
Thomas Pitiot committed
237

pitiot's avatar
up  
pitiot committed
238
    REAL rayon = sim_->envMap_.volumeMaxdistance(volume) ;
Thomas Pitiot 's avatar
Thomas Pitiot committed
239
    if(rayon > sim_->envMap_.maxCellRay )
pitiot's avatar
up  
pitiot committed
240
    {
Thomas Pitiot 's avatar
Thomas Pitiot committed
241 242 243 244
//        if(sim_->envMap_.subdivideVolume(volume))
//        {
//            refine();
//        }
pitiot's avatar
up  
pitiot committed
245 246
    }

Thomas Pitiot 's avatar
Thomas Pitiot committed
247
#endif
pitiot's avatar
up  
pitiot committed
248 249

}