source: src/Tesselation/triangleintersectionlist.cpp@ b4f72c

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since b4f72c was 94d5ac6, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: As we use GSL internally, we are as of now required to use GPL v2 license.

  • GNU Scientific Library is used at every place in the code, especially the sub-package LinearAlgebra is based on it which in turn is used really everywhere in the remainder of MoleCuilder. Hence, we have to use the GPL license for the whole of MoleCuilder. In effect, GPL's COPYING was present all along and stated the terms of the GPL v2 license.
  • Hence, I added the default GPL v2 disclaimer to every source file and removed the note about a (actually missing) LICENSE file.
  • also, I added a help-redistribute action which again gives the disclaimer of the GPL v2.
  • also, I changed in the disclaimer that is printed at every program start in builder_init.cpp.
  • TEST: Added check on GPL statement present in every module to test CodeChecks project-disclaimer.
  • Property mode set to 100644
File size: 9.3 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * triangleintersectionlist.cpp
25 *
26 * This files encapsulates the TriangleIntersectionList class where all matters related to points in space being how close to
27 * a tesselated surface are answered, i.e. distance, is inside, ...
28 *
29 * Created on: Mar 1, 2010
30 * Author: heber
31 */
32
33// include config.h
34#ifdef HAVE_CONFIG_H
35#include <config.h>
36#endif
37
38#include "CodePatterns/MemDebug.hpp"
39
40#include <boost/scoped_ptr.hpp>
41
42#include "triangleintersectionlist.hpp"
43
44#include "tesselation.hpp"
45#include "BoundaryMaps.hpp"
46#include "BoundaryPointSet.hpp"
47#include "BoundaryLineSet.hpp"
48#include "BoundaryTriangleSet.hpp"
49#include "CodePatterns/Info.hpp"
50#include "CodePatterns/Log.hpp"
51#include "CodePatterns/Verbose.hpp"
52#include "Helpers/defs.hpp"
53#include "LinearAlgebra/Vector.hpp"
54
55/** Constructor for class TriangleIntersectionList.
56 *
57 */
58TriangleIntersectionList::TriangleIntersectionList(const Vector &x, const Tesselation *surface, const LinkedCell_deprecated* LC) :
59 Point(x),
60 Tess(surface),
61 Vicinity(LC)
62{
63 //Info FunctionInfo(__func__);
64 GatherIntersectionsWithTriangles();
65};
66
67/** Destructor for class TriangleIntersectionList.
68 * Free's all allocated intersection vectors
69 */
70TriangleIntersectionList::~TriangleIntersectionList()
71{
72 //Info FunctionInfo(__func__);
73 for (TriangleVectorMap::iterator Runner = IntersectionList.begin(); Runner != IntersectionList.end(); Runner++)
74 delete((*Runner).second);
75};
76
77/** Returns the smallest distance between TriangleIntersectionList::Point and the surface given by
78 * TriangleIntersectionList::Tess.
79 * \return distance >=0, -1 if no specific distance could be found
80 */
81double TriangleIntersectionList::GetSmallestDistance() const
82{
83 //Info FunctionInfo(__func__);
84 FillDistanceList();
85 if (!DistanceList.empty())
86 return DistanceList.begin()->first;
87 else
88 // return reference, if none can be found
89 return -1.;
90};
91
92/** Returns the vector of closest intersection between TriangleIntersectionList::Point and the surface
93 * TriangleIntersectionList::Tess.
94 * \return Intersection vector or TriangleIntersectionList::Point if none could be found
95 */
96Vector TriangleIntersectionList::GetClosestIntersection() const
97{
98 //Info FunctionInfo(__func__);
99 TriangleVectorMap::const_iterator runner;
100 runner = GetIteratortoSmallestDistance();
101 if (runner != IntersectionList.end()) {
102 return *((*runner).second);
103 }
104 // return reference, if none can be found
105 return Point;
106};
107
108/** Returns the triangle closest to TriangleIntersectionList::Point and the surface
109 * TriangleIntersectionList::Tess.
110 * \return pointer to triangle or NULL if none could be found
111 */
112BoundaryTriangleSet * TriangleIntersectionList::GetClosestTriangle() const
113{
114 //Info FunctionInfo(__func__);
115 TriangleVectorMap::const_iterator runner;
116 runner = GetIteratortoSmallestDistance();
117 if (runner != IntersectionList.end()) {
118 return ((*runner).first);
119 }
120 // return reference, if none can be found
121 return NULL;
122};
123
124/** Checks whether reference TriangleIntersectionList::Point of this class is inside or outside.
125 * \return true - TriangleIntersectionList::Point is inside, false - is outside
126 */
127bool TriangleIntersectionList::IsInside() const
128{
129 //Info FunctionInfo(__func__);
130 TriangleVectorMap::const_iterator runner = GetIteratortoSmallestDistance();
131 if (runner != IntersectionList.end()) {
132 // if we have found one, check Scalarproduct between the vector
133 Vector TestVector = (Point) - (*(*runner).second);
134 LOG(1, "INFO: Distance vector between point " << Point
135 << " and triangle intersection at " << (*(*runner).second) << " is "
136 << TestVector << ".");
137 if (fabs(TestVector.NormSquared()) < MYEPSILON) {//
138 LOG(1, "ACCEPT: Point is on the intersected triangle.");
139 return true;
140 }
141 const double sign = (*runner).first->NormalVector.ScalarProduct(TestVector);
142 LOG(1, "INFO: Checking sign of SKP between Distance vector and triangle's NormalVector "
143 << (*runner).first->NormalVector << ": " << sign << ".");
144 if (sign < 0) {
145 LOG(1, "ACCEPT: Point lies on inner side of intersected triangle.");
146 return true;
147 } else {
148 LOG(1, "REJECT: Point lies on outer side of intersected triangle.");
149 return false;
150 }
151 }
152 // so far away from surface that there are no triangles in list
153 return false;
154};
155
156/** Puts all triangles in vicinity (by \a *LC) into a hash map with Intersection vectors.
157 * Private function for constructor of TriangleIntersectionList.
158 */
159void TriangleIntersectionList::GatherIntersectionsWithTriangles()
160{
161 //Info FunctionInfo(__func__);
162
163 // get closest points
164 boost::scoped_ptr< DistanceToPointMap > points(Tess->FindClosestBoundaryPointsToVector(Point,Vicinity));
165 if (points == NULL) {
166 ELOG(1, "There is no nearest point: too far away from the surface.");
167 return;
168 }
169
170 // gather all triangles in *LC-neighbourhood
171 TriangleSet triangles;
172 for (DistanceToPointMap::iterator Runner = points->begin(); Runner != points->end(); Runner++)
173 for (LineMap::iterator LineRunner = Runner->second->lines.begin(); LineRunner != Runner->second->lines.end(); LineRunner++)
174 for (TriangleMap::iterator TriangleRunner = LineRunner->second->triangles.begin(); TriangleRunner != LineRunner->second->triangles.end(); TriangleRunner++)
175 triangles.insert(TriangleRunner->second);
176
177 Vector *Intersection = NULL;
178 for (TriangleSet::const_iterator TriangleRunner = triangles.begin(); TriangleRunner != triangles.end(); TriangleRunner++) {
179 // get intersection with triangle plane
180 Intersection = new Vector;
181 (*TriangleRunner)->GetClosestPointInsideTriangle(Point, *Intersection);
182 //LOG(1, "Intersection between " << Point << " and " << **TriangleRunner << " is at " << *Intersection << ".");
183 IntersectionList.insert( pair<BoundaryTriangleSet *, Vector * > (*TriangleRunner, Intersection) );
184 }
185};
186
187/** Fills the private distance list
188 */
189void TriangleIntersectionList::FillDistanceList() const
190{
191 //Info FunctionInfo(__func__);
192 if (DistanceList.empty())
193 for (TriangleVectorMap::const_iterator runner = IntersectionList.begin(); runner != IntersectionList.end(); runner++)
194 DistanceList.insert( pair<double, BoundaryTriangleSet *> (Point.distance(*(*runner).second), (*runner).first) );
195
196 //for (DistanceTriangleMap::const_iterator runner = DistanceList.begin(); runner != DistanceList.end(); runner++)
197 // LOG(1, (*runner).first << " away from " << *(*runner).second);
198};
199
200
201/** Returns the iterator in VectorTriangleMap to the smallest distance.
202 * This is a private helper function as the iterator is then evaluated in some other functions.
203 * \return iterator or TriangleIntersectionList::IntersectionList.end() if none could be found
204 */
205TriangleVectorMap::const_iterator TriangleIntersectionList::GetIteratortoSmallestDistance() const
206{
207 //Info FunctionInfo(__func__);
208 FillDistanceList();
209
210 // do we have any intersections?
211 TriangleVectorMap::const_iterator runner = IntersectionList.end();
212 if (!DistanceList.empty()) {
213 // get closest triangle(s)
214 std::pair< DistanceTriangleMap::const_iterator, DistanceTriangleMap::const_iterator > DistanceRange =
215 DistanceList.equal_range(DistanceList.begin()->first);
216 {
217 size_t count = 0;
218 for (DistanceTriangleMap::const_iterator iter = DistanceRange.first;
219 iter != DistanceRange.second; ++iter)
220 ++count;
221 LOG(1, "INFO: There are " << count << " possible triangles at the smallest distance.");
222 }
223 // if there is more than one, check all of their normal vectors
224 // return the one with positive SKP if present because if the point
225 // would be truely inside, all of the closest triangles would have to show
226 // their inner side
227 LOG(1, "INFO: Looking for first SKP greater than zero ...");
228 for (DistanceTriangleMap::const_iterator iter = DistanceRange.first;
229 iter != DistanceRange.second; ++iter) {
230 // find the entry by the triangle key in IntersectionList to get the Intersection point
231 runner = IntersectionList.find(iter->second);
232 Vector TestVector = (Point) - (*(*runner).second);
233 // construct SKP
234 const double sign = (*iter).second->NormalVector.ScalarProduct(TestVector);
235 LOG(1, "INFO: Checking SKP of closest triangle " << *(iter->second) << " = " << sign << ".");
236 // if positive return
237 if (sign > 0)
238 break;
239 }
240 // if there's just one or all are negative, runner is not set yet
241 if (runner == IntersectionList.end())
242 runner = IntersectionList.find(DistanceList.begin()->second);
243 }
244 return runner;
245};
246
Note: See TracBrowser for help on using the repository browser.