|
| 1 | +/***************************************************************************** |
| 2 | + * - Copyright (C) - 2020 - InfinyTech3D - * |
| 3 | + * * |
| 4 | + * This file is part of the InfinyToolkit plugin for the SOFA framework * |
| 5 | + * * |
| 6 | + * Commercial License Usage: * |
| 7 | + * Licensees holding valid commercial license from InfinyTech3D may use this * |
| 8 | + * file in accordance with the commercial license agreement provided with * |
| 9 | + * the Software or, alternatively, in accordance with the terms contained in * |
| 10 | + * a written agreement between you and InfinyTech3D. For further information * |
| 11 | + * on the licensing terms and conditions, contact: contact@infinytech3d.com * |
| 12 | + * * |
| 13 | + * GNU General Public License Usage: * |
| 14 | + * Alternatively, this file may be used under the terms of the GNU General * |
| 15 | + * Public License version 3. The licenses are as published by the Free * |
| 16 | + * Software Foundation and appearing in the file LICENSE.GPL3 included in * |
| 17 | + * the packaging of this file. Please review the following information to * |
| 18 | + * ensure the GNU General Public License requirements will be met: * |
| 19 | + * https://www.gnu.org/licenses/gpl-3.0.html. * |
| 20 | + * * |
| 21 | + * Authors: see Authors.txt * |
| 22 | + * Further information: https://infinytech3d.com * |
| 23 | + ****************************************************************************/ |
| 24 | + |
| 25 | +#include <InfinyToolkit/CollisionDetectionDisplay.h> |
| 26 | +#include <sofa/core/visual/VisualParams.h> |
| 27 | +#include <sofa/core/ObjectFactory.h> |
| 28 | +#include <sofa/simulation/AnimateEndEvent.h> |
| 29 | +#include <limits> |
| 30 | + |
| 31 | +namespace sofa::infinytoolkit |
| 32 | +{ |
| 33 | + |
| 34 | +int CollisionDetectionDisplayClass = core::RegisterObject("Class to track the current position of the needle tip.") |
| 35 | + .add< CollisionDetectionDisplay >() |
| 36 | + ; |
| 37 | + |
| 38 | + |
| 39 | +CollisionDetectionDisplay::CollisionDetectionDisplay() |
| 40 | + : d_drawContacts(initData(&d_drawContacts, false, "drawContacts", "if true, will draw slices BB, ray and intersected triangles")) |
| 41 | + , d_collisionPoints(initData(&d_collisionPoints, "collisionPoints", "vector of pair of collision points")) |
| 42 | +{ |
| 43 | + f_listening.setValue(true); |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +CollisionDetectionDisplay::~CollisionDetectionDisplay() |
| 48 | +{ |
| 49 | + clearContacts(); |
| 50 | +} |
| 51 | + |
| 52 | +void CollisionDetectionDisplay::init() |
| 53 | +{ |
| 54 | + sofa::core::objectmodel::BaseObject::d_componentState.setValue(sofa::core::objectmodel::ComponentState::Loading); |
| 55 | + |
| 56 | + // If no NarrowPhaseDetection is set using the link try to find the component |
| 57 | + if (l_detectionNP.get() == nullptr) |
| 58 | + { |
| 59 | + l_detectionNP.set(getContext()->get<core::collision::NarrowPhaseDetection>()); |
| 60 | + } |
| 61 | + |
| 62 | + if (l_detectionNP.get() == nullptr) { |
| 63 | + msg_error() << "NarrowPhaseDetection not found"; |
| 64 | + sofa::core::objectmodel::BaseObject::d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid); |
| 65 | + } |
| 66 | + |
| 67 | + sofa::core::objectmodel::BaseObject::d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid); |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +void CollisionDetectionDisplay::handleEvent(sofa::core::objectmodel::Event* event) |
| 72 | +{ |
| 73 | + if (d_componentState.getValue() != sofa::core::objectmodel::ComponentState::Valid) |
| 74 | + return; |
| 75 | + |
| 76 | + if (simulation::AnimateEndEvent::checkEventType(event)) |
| 77 | + { |
| 78 | + filterCollision(); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +void CollisionDetectionDisplay::filterCollision() |
| 84 | +{ |
| 85 | + if (!this->isComponentStateValid()) |
| 86 | + return; |
| 87 | + |
| 88 | + clearContacts(); |
| 89 | + |
| 90 | + // get the collision output |
| 91 | + const core::collision::NarrowPhaseDetection::DetectionOutputMap& detectionOutputs = l_detectionNP.get()->getDetectionOutputs(); |
| 92 | + if (detectionOutputs.size() == 0) // exit if no collision |
| 93 | + { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + // loop on the contact to get the one between the CarvingSurface and the CarvingTool collision model |
| 98 | + const ContactVector* contacts = nullptr; |
| 99 | + auto colPoints = sofa::helper::getWriteAccessor(d_collisionPoints); |
| 100 | + for (core::collision::NarrowPhaseDetection::DetectionOutputMap::const_iterator it = detectionOutputs.begin(); it != detectionOutputs.end(); ++it) |
| 101 | + { |
| 102 | + sofa::core::CollisionModel* collMod1 = it->first.first; |
| 103 | + sofa::core::CollisionModel* collMod2 = it->first.second; |
| 104 | + |
| 105 | + dmsg_info() << "collMod1: " << collMod1->getTypeName() << " -> " << collMod1->getContext()->getName(); |
| 106 | + dmsg_info() << "collMod1: " << collMod2->getTypeName() << " -> " << collMod2->getContext()->getName(); |
| 107 | + |
| 108 | + |
| 109 | + // Get the number of contacts |
| 110 | + contacts = dynamic_cast<const ContactVector*>(it->second); |
| 111 | + if (contacts == nullptr) |
| 112 | + continue; |
| 113 | + |
| 114 | + size_t ncontacts = contacts->size(); |
| 115 | + if (contacts->size() == 0) |
| 116 | + continue; |
| 117 | + |
| 118 | + for (size_t j = 0; j < ncontacts; ++j) |
| 119 | + { |
| 120 | + const ContactVector::value_type& c = (*contacts)[j]; |
| 121 | + // update the triangle id if a mapping is present |
| 122 | + contactInfo* info = new contactInfo(); |
| 123 | + |
| 124 | + dmsg_info() << j << " contact: " << c.elem.first.getIndex() << " | " << c.elem.second.getIndex() |
| 125 | + << " -> " << " pA: " << c.point[0] << " pB: " << c.point[1] |
| 126 | + << " | normal: " << c.normal << " d: " << c.value |
| 127 | + << " | cDir: " << (c.point[1] - c.point[0]).normalized() << " d: " << (c.point[1] - c.point[0]).norm(); |
| 128 | + |
| 129 | + info->idA = c.elem.first.getIndex(); |
| 130 | + info->idB = c.elem.second.getIndex(); |
| 131 | + info->normal = c.normal; |
| 132 | + info->pointA = c.point[0]; |
| 133 | + info->pointB = c.point[1]; |
| 134 | + info->dist = c.value; |
| 135 | + |
| 136 | + m_contactInfos.push_back(info); |
| 137 | + colPoints.push_back(info->pointA); |
| 138 | + colPoints.push_back(info->pointB); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +void CollisionDetectionDisplay::clearContacts() |
| 144 | +{ |
| 145 | + for (unsigned int i = 0; i < m_contactInfos.size(); i++) |
| 146 | + { |
| 147 | + delete m_contactInfos[i]; |
| 148 | + m_contactInfos[i] = nullptr; |
| 149 | + } |
| 150 | + m_contactInfos.clear(); |
| 151 | + |
| 152 | + auto points = sofa::helper::getWriteAccessor(d_collisionPoints); |
| 153 | + points.clear(); |
| 154 | +} |
| 155 | + |
| 156 | + |
| 157 | +void CollisionDetectionDisplay::draw(const core::visual::VisualParams* vparams) |
| 158 | +{ |
| 159 | + if (!this->isComponentStateValid() || !d_drawContacts.getValue()) |
| 160 | + return; |
| 161 | + |
| 162 | + const auto stateLifeCycle = vparams->drawTool()->makeStateLifeCycle(); |
| 163 | + for (contactInfo* cInfo : m_contactInfos) |
| 164 | + { |
| 165 | + std::vector<Vec3> vertices; |
| 166 | + vertices.push_back(cInfo->pointA); |
| 167 | + vertices.push_back(cInfo->pointB); |
| 168 | + |
| 169 | + sofa::type::RGBAColor color4(1.0f, 1.0, 0.0f, 1.0); |
| 170 | + |
| 171 | + vparams->drawTool()->drawLines(vertices, 1, color4); |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | +} // namespace sofa::infinytoolkit |
0 commit comments