1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 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 220
|
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Modules *
* *
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include "CarvingManager.h"
#include <sofa/core/ObjectFactory.h>
#include <sofa/core/componentmodel/collision/DetectionOutput.h>
#include <sofa/core/objectmodel/KeypressedEvent.h>
#include <sofa/core/objectmodel/KeyreleasedEvent.h>
#include <sofa/simulation/common/AnimateBeginEvent.h>
#include <sofa/simulation/common/AnimateEndEvent.h>
#include <sofa/core/componentmodel/topology/TopologicalMapping.h>
#include <sofa/helper/gl/template.h>
#include <sofa/component/collision/TopologicalChangeManager.h>
namespace sofa
{
namespace component
{
namespace collision
{
SOFA_DECL_CLASS(CarvingManager)
int CarvingManagerClass = core::RegisterObject("Manager handling carving operations between a tool and an object.")
.add< CarvingManager >()
;
CarvingManager::CarvingManager()
: f_modelTool( initData(&f_modelTool, "modelTool", "Tool model path"))
, f_modelSurface( initData(&f_modelSurface, "modelSurface", "TriangleSetModel or SphereModel path"))
, active( initData(&active, false, "active", "Activate this object.\nNote that this can be dynamically controlled by using a key") )
, keyEvent( initData(&keyEvent, '1', "key", "key to press to activate this object until the key is released") )
, keySwitchEvent( initData(&keySwitchEvent, '4', "keySwitch", "key to activate this object until the key is pressed again") )
, modelTool(NULL)
, modelSurface(NULL)
, intersectionMethod(NULL)
, detectionNP(NULL)
{
this->f_listening.setValue(true);
}
CarvingManager::~CarvingManager()
{
}
void CarvingManager::init()
{
if (f_modelTool.getValue().empty())
modelTool = getContext()->get<ToolModel>(core::objectmodel::BaseContext::SearchDown);
else
modelTool = getContext()->get<ToolModel>(f_modelTool.getValue());
if (f_modelSurface.getValue().empty())
{
// we look for a CollisionModel relying on a TetrahedronSetTopology.
//modelSurface = getContext()->get<TriangleSetModel>(core::objectmodel::BaseContext::SearchDown);
std::vector<core::CollisionModel*> models;
getContext()->get<core::CollisionModel>(&models, core::objectmodel::BaseContext::SearchRoot);
sofa::core::componentmodel::topology::TopologicalMapping * topoMapping;
for (unsigned int i=0;i<models.size();++i)
{
core::CollisionModel* m = models[i];
m->getContext()->get(topoMapping);
if (topoMapping == NULL) continue;
modelSurface = m; // we found a good object
break;
}
}
else
{
modelSurface = getContext()->get<core::CollisionModel>(f_modelSurface.getValue());
}
intersectionMethod = getContext()->get<core::componentmodel::collision::Intersection>();
detectionNP = getContext()->get<core::componentmodel::collision::NarrowPhaseDetection>();
bool error = false;
if (modelTool == NULL) { serr << "CarvingManager: modelTool not found"<<sendl; error = true; }
if (modelSurface == NULL) { serr << "CarvingManager: modelSurface not found"<<sendl; error = true; }
if (intersectionMethod == NULL) { serr << "CarvingManager: intersectionMethod not found"<<sendl; error = true; }
if (detectionNP == NULL) { serr << "CarvingManager: NarrowPhaseDetection not found"<<sendl; error = true; }
if (!error)
sout << "CarvingManager: init OK." << sendl;
}
void CarvingManager::reset()
{
}
void CarvingManager::doCarve()
{
if (modelTool==NULL || modelSurface==NULL || intersectionMethod == NULL || detectionNP == NULL) return;
// do collision detection
//sout << "CarvingManager: build bounding trees" << sendl;
{
const bool continuous = intersectionMethod->useContinuous();
const double dt = getContext()->getDt();
const int depth = 6;
if (continuous)
modelTool->computeContinuousBoundingTree(dt, depth);
else
modelTool->computeBoundingTree(depth);
if (continuous)
modelSurface->computeContinuousBoundingTree(dt, depth);
else
modelSurface->computeBoundingTree(depth);
}
sofa::helper::vector<std::pair<core::CollisionModel*, core::CollisionModel*> > vectCMPair;
//vectCMPair = broadPhaseDetection->getCollisionModelPairs();
//vectCMPair.push_back(std::make_pair(modelSurface,modelTool));
vectCMPair.push_back(std::make_pair(modelSurface->getFirst(),modelTool->getFirst()));
//sout << "CarvingManager: narrow phase" << sendl;
detectionNP->setInstance(this);
detectionNP->setIntersectionMethod(intersectionMethod);
detectionNP->beginNarrowPhase();
detectionNP->addCollisionPairs(vectCMPair);
detectionNP->endNarrowPhase();
const core::componentmodel::collision::NarrowPhaseDetection::DetectionOutputMap& detectionOutputs = detectionNP->getDetectionOutputs();
//sout << "CarvingManager: process contacts" << sendl;
const ContactVector* contacts = NULL;
core::componentmodel::collision::NarrowPhaseDetection::DetectionOutputMap::const_iterator it = detectionOutputs.begin(); //find(std::make_pair(modelSurface,modelTool));
if (it != detectionOutputs.end())
{
#ifndef NDEBUG
sout << "Contacts available..." << sendl;
#endif
contacts = dynamic_cast<const ContactVector*>(it->second);
}
unsigned int ncontacts = 0;
if (contacts != NULL)
{
ncontacts = contacts->size();
#ifndef NDEBUG
sout << contacts->size() << " contacts detected." << sendl;
#endif
}
std::vector<int> elemsToRemove;
for (unsigned int j=0; j < ncontacts; ++j)
{
const ContactVector::value_type& c = (*contacts)[j];
int triangleIdx = (c.elem.first.getCollisionModel()==modelSurface ? c.elem.first.getIndex():c.elem.second.getIndex());
elemsToRemove.push_back(triangleIdx);
}
if (!elemsToRemove.empty())
{
#ifndef NDEBUG
sout << elemsToRemove.size() << " elements to remove"<<sendl;
#endif
static TopologicalChangeManager manager;
manager.removeItemsFromCollisionModel(modelSurface, elemsToRemove);
}
detectionNP->setInstance(NULL);
//sout << "CarvingManager: carve done" << sendl;
}
void CarvingManager::handleEvent(sofa::core::objectmodel::Event* event)
{
if (sofa::core::objectmodel::KeypressedEvent* ev = dynamic_cast<sofa::core::objectmodel::KeypressedEvent*>(event))
{
sout << "GET KEY "<<ev->getKey()<<sendl;
if (ev->getKey() == keyEvent.getValue())
{
active.setValue(true);
}
else if (ev->getKey() == keySwitchEvent.getValue())
{
active.setValue(!active.getValue());
}
}
else if (sofa::core::objectmodel::KeyreleasedEvent* ev = dynamic_cast<sofa::core::objectmodel::KeyreleasedEvent*>(event))
{
if (ev->getKey() == keyEvent.getValue())
{
active.setValue(false);
}
}
else if (/* simulation::AnimateEndEvent* ev = */ dynamic_cast<simulation::AnimateEndEvent*>(event))
{
if (active.getValue())
doCarve();
}
}
} // namespace collision
} // namespace component
} // namespace sofa
|