Skip to content

Commit 293c786

Browse files
committed
first try transformation matrix
1 parent 6428ab7 commit 293c786

File tree

8 files changed

+208
-34
lines changed

8 files changed

+208
-34
lines changed

Contact.cxx

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,43 @@ Contact::Contact (vtkPolyData *newPdA, vtkPolyData *newPdB) : newPdA(newPdA), ne
164164

165165
GetNonManifoldEdges(newPdA, edgesA);
166166
GetNonManifoldEdges(newPdB, edgesB);
167-
}
168167

169-
vtkSmartPointer<vtkPolyData> Contact::GetLines () {
170-
auto treeA = vtkSmartPointer<vtkOBBTree>::New();
168+
treeA = vtkSmartPointer<vtkOBBTree>::New();
171169
treeA->SetDataSet(newPdA);
172170
treeA->BuildLocator();
173171

174-
auto treeB = vtkSmartPointer<vtkOBBTree>::New();
172+
treeB = vtkSmartPointer<vtkOBBTree>::New();
175173
treeB->SetDataSet(newPdB);
176174
treeB->BuildLocator();
175+
}
176+
177+
vtkSmartPointer<vtkPolyData> Contact::GetLines (vtkPolyData *pdA, vtkLinearTransform *transA, vtkPolyData *pdB, vtkLinearTransform *transB) {
178+
179+
auto matrix = vtkSmartPointer<vtkMatrix4x4>::New();
180+
181+
if (pdA != nullptr) {
182+
newPdA = pdA;
183+
}
184+
185+
if (pdB != nullptr) {
186+
newPdB = pdB;
187+
}
177188

178-
treeA->IntersectWithOBBTree(treeB, nullptr, InterNodes, this);
189+
if (transA != nullptr || transB != nullptr) {
190+
auto tmpMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
191+
192+
vtkMatrix4x4::Invert(transA->GetMatrix(), tmpMatrix);
193+
vtkMatrix4x4::Multiply4x4(tmpMatrix, transB->GetMatrix(), matrix);
194+
}
195+
196+
sourcesA->Reset();
197+
sourcesB->Reset();
198+
contA->Reset();
199+
contB->Reset();
200+
201+
lines->Reset();
202+
203+
treeA->IntersectWithOBBTree(treeB, matrix, InterNodes, this);
179204

180205
IntersectReplacements();
181206

@@ -572,13 +597,8 @@ void Contact::InterPolys (vtkIdType idA, vtkIdType idB) {
572597
return;
573598
}
574599

575-
double ptA[3], ptB[3];
576-
577-
newPdA->GetPoint(ptsA[0], ptA);
578-
newPdB->GetPoint(ptsB[0], ptB);
579-
580-
double dA = vtkMath::Dot(nA, ptA);
581-
double dB = vtkMath::Dot(nB, ptB);
600+
double dA = polyA[0].x*nA[0]+polyA[0].y*nA[1]+polyA[0].z*nA[2];
601+
double dB = polyB[0].x*nB[0]+polyB[0].y*nB[1]+polyB[0].z*nB[2];
582602

583603
vtkMath::Cross(nA, nB, r);
584604
vtkMath::Normalize(r);

Contact.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License.
2121

2222
#include <vtkOBBTree.h>
2323
#include <vtkMatrix4x4.h>
24+
#include <vtkLinearTransform.h>
2425

2526
enum class Src {
2627
A,
@@ -123,7 +124,9 @@ class Contact {
123124

124125
NonManifoldEdgesType edgesA, edgesB;
125126

126-
vtkSmartPointer<vtkPolyData> GetLines ();
127+
vtkSmartPointer<vtkOBBTree> treeA, treeB;
128+
129+
vtkSmartPointer<vtkPolyData> GetLines (vtkPolyData *pdA = nullptr, vtkLinearTransform *transA = nullptr, vtkPolyData *pdB = nullptr, vtkLinearTransform *transB = nullptr);
127130

128131
void GetNonManifoldEdges (vtkPolyData *pd, NonManifoldEdgesType &edges);
129132

Utilities.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class Point3d {
9090

9191
return dx*dx+dy*dy+dz*dz;
9292
}
93+
94+
9395
};
9496

9597
typedef std::vector<vtkIdType> IdsType;

transform/bone.vtk

5.87 MB
Binary file not shown.

transform/box.vtk

1.52 MB
Binary file not shown.

transform/test.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
# *-* coding: UTF-8 *-*
3+
4+
import sys
5+
sys.path.extend(['/home/zippy/vtkbool/build/lib/python3.13/site-packages/vtkbool'])
6+
7+
from vtkmodules.vtkIOLegacy import vtkPolyDataReader, vtkPolyDataWriter
8+
from vtkmodules.vtkCommonMath import vtkMatrix4x4
9+
from vtkmodules.vtkCommonTransforms import vtkTransform
10+
from vtkmodules.vtkFiltersGeneral import vtkTransformPolyDataFilter
11+
12+
from vtkbool import vtkPolyDataBooleanFilter
13+
14+
mandibleReader = vtkPolyDataReader()
15+
mandibleReader.SetFileName('bone.vtk')
16+
mandibleReader.Update()
17+
18+
boxReader = vtkPolyDataReader()
19+
boxReader.SetFileName('box.vtk')
20+
boxReader.Update()
21+
22+
moveBoxTransform = vtkTransform()
23+
moveBoxTransform.Translate(20.076142063093318, -8.288617300972092, -53.29349952031101)
24+
moveBoxMatrix = moveBoxTransform.GetMatrix()
25+
26+
identityMatrix = vtkMatrix4x4()
27+
28+
bf = vtkPolyDataBooleanFilter()
29+
bf.SetInputData(0, mandibleReader.GetOutput())
30+
bf.SetInputData(1, boxReader.GetOutput())
31+
32+
bf.SetMatrix(0, identityMatrix)
33+
bf.SetMatrix(1, moveBoxMatrix)
34+
35+
for i in range(10):
36+
moveBoxTransform.Translate(0, -10, 0)
37+
moveBoxMatrix = moveBoxTransform.GetMatrix()
38+
39+
print(moveBoxMatrix.GetElement(1, 3))
40+
41+
transformFilter = vtkTransformPolyDataFilter()
42+
transformFilter.SetInputData(boxReader.GetOutput())
43+
transformFilter.SetTransform(moveBoxTransform)
44+
transformFilter.Update()
45+
46+
_writer = vtkPolyDataWriter()
47+
_writer.SetFileName(f'out/pdB_{i}.vtk')
48+
_writer.SetInputConnection(transformFilter.GetOutputPort())
49+
_writer.Update()
50+
51+
bf.SetMatrix(1, moveBoxMatrix)
52+
53+
writer = vtkPolyDataWriter()
54+
writer.SetFileName(f'out/transformed_{i}.vtk')
55+
writer.SetInputConnection(bf.GetOutputPort())
56+
57+
writer.Update()

vtkPolyDataBooleanFilter.cxx

Lines changed: 99 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ limitations under the License.
4747
#include <vtkCellArrayIterator.h>
4848
#include <vtkKdTree.h>
4949
#include <vtkCellIterator.h>
50+
#include <vtkTransformPolyDataFilter.h>
5051

5152
#include "vtkPolyDataBooleanFilter.h"
5253

@@ -78,10 +79,30 @@ vtkPolyDataBooleanFilter::vtkPolyDataBooleanFilter () {
7879

7980
OperMode = OPER_UNION;
8081

82+
matrices[0] = nullptr;
83+
matrices[1] = nullptr;
84+
85+
transforms[0] = nullptr;
86+
transforms[1] = nullptr;
87+
8188
}
8289

8390
vtkPolyDataBooleanFilter::~vtkPolyDataBooleanFilter () {
84-
// nix mehr
91+
if (matrices[0] != nullptr) {
92+
matrices[0]->Delete();
93+
}
94+
95+
if (matrices[1] != nullptr) {
96+
matrices[1]->Delete();
97+
}
98+
99+
if (transforms[0] != nullptr) {
100+
transforms[0]->Delete();
101+
}
102+
103+
if (transforms[1] != nullptr) {
104+
transforms[1]->Delete();
105+
}
85106
}
86107

87108
int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) {
@@ -106,36 +127,63 @@ int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformatio
106127
std::vector<clock::duration> times;
107128
clock::time_point start;
108129

109-
if (pdA->GetMTime() > timePdA || pdB->GetMTime() > timePdB) {
130+
// if (pdA->GetMTime() > timePdA || pdB->GetMTime() > timePdB)
131+
{
110132
// CellData sichern
111133

112-
cellDataA->DeepCopy(pdA->GetCellData());
113-
cellDataB->DeepCopy(pdB->GetCellData());
134+
if (contact == nullptr) {
135+
cellDataA->DeepCopy(pdA->GetCellData());
136+
cellDataB->DeepCopy(pdB->GetCellData());
114137

115-
modPdA = Clean(pdA);
116-
modPdB = Clean(pdB);
138+
cleanA = Clean(pdA);
139+
cleanB = Clean(pdB);
117140

118-
modPdA->EditableOn();
119-
modPdB->EditableOn();
141+
#ifdef DEBUG
142+
WriteVTK("modPdA.vtk", cleanA);
143+
WriteVTK("modPdB.vtk", cleanB);
144+
#endif
120145

121-
#ifdef DEBUG
122-
WriteVTK("modPdA.vtk", modPdA);
123-
WriteVTK("modPdB.vtk", modPdB);
124-
#endif
146+
try {
147+
PreventEqualCaptPoints(cleanA, cleanB).Run();
148+
} catch (const std::runtime_error &e) {
149+
vtkErrorMacro("Cannot prevent equal capture points.");
150+
return 1;
151+
}
125152

126-
try {
127-
PreventEqualCaptPoints(modPdA, modPdB).Run();
128-
} catch (const std::runtime_error &e) {
129-
vtkErrorMacro("Cannot prevent equal capture points.");
130-
return 1;
153+
contact = std::make_shared<Contact>(cleanA, cleanB);
131154
}
132155

133156
start = clock::now();
134157

135-
Contact contact(modPdA, modPdB);
158+
if (transforms[0] != nullptr) {
159+
auto tfA = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
160+
tfA->SetInputData(cleanA);
161+
tfA->SetTransform(transforms[0]);
162+
tfA->Update();
163+
164+
modPdA = vtkSmartPointer<vtkPolyData>::New();
165+
modPdA->DeepCopy(tfA->GetOutput());
166+
} else {
167+
modPdA = cleanA;
168+
}
169+
170+
if (transforms[1] != nullptr) {
171+
auto tfB = vtkSmartPointer<vtkTransformPolyDataFilter>::New();
172+
tfB->SetInputData(cleanB);
173+
tfB->SetTransform(transforms[1]);
174+
tfB->Update();
175+
176+
modPdB = vtkSmartPointer<vtkPolyData>::New();
177+
modPdB->DeepCopy(tfB->GetOutput());
178+
} else {
179+
modPdB = cleanB;
180+
}
181+
182+
modPdA->EditableOn();
183+
modPdB->EditableOn();
136184

137185
try {
138-
contLines = contact.GetLines();
186+
contLines = contact->GetLines(modPdA, transforms[0], modPdB, transforms[1]);
139187
} catch (const std::runtime_error &e) {
140188
std::stringstream ss;
141189
ss << std::quoted(e.what());
@@ -154,7 +202,7 @@ int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformatio
154202
#endif
155203

156204
if (contLines->GetNumberOfCells() == 0) {
157-
vtkErrorMacro("There is no contact.");
205+
vtkErrorMacro("There is no contact 1.");
158206
return 1;
159207
}
160208

@@ -210,7 +258,7 @@ int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformatio
210258
// löscht bestimmte strips
211259

212260
if (CleanStrips()) {
213-
vtkErrorMacro("There is no contact.");
261+
vtkErrorMacro("There is no contact 2.");
214262
return 1;
215263
}
216264

@@ -3024,3 +3072,33 @@ bool vtkPolyDataBooleanFilter::CombineRegions () {
30243072
return false;
30253073

30263074
}
3075+
3076+
void vtkPolyDataBooleanFilter::SetMatrix (int i, vtkMatrix4x4 *matrix) {
3077+
if (transforms[i]) {
3078+
transforms[i]->Delete();
3079+
transforms[i] = nullptr;
3080+
}
3081+
3082+
if (matrices[i]) {
3083+
matrices[i]->Delete();
3084+
matrices[i] = nullptr;
3085+
}
3086+
3087+
matrices[i] = matrix;
3088+
matrix->Register(this);
3089+
3090+
auto transform = vtkMatrixToLinearTransform::New();
3091+
3092+
transform->Register(this);
3093+
transform->Delete();
3094+
3095+
transform->SetInput(matrix);
3096+
3097+
transforms[i] = transform;
3098+
3099+
Modified();
3100+
}
3101+
3102+
vtkMatrix4x4* vtkPolyDataBooleanFilter::GetMatrix (int i) {
3103+
return matrices[i];
3104+
}

vtkPolyDataBooleanFilter.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ limitations under the License.
2828
#include <vtkPolyDataAlgorithm.h>
2929
#include <vtkKdTree.h>
3030
#include <vtkModifiedBSPTree.h>
31+
#include <vtkMatrixToLinearTransform.h>
32+
#include <vtkMatrix4x4.h>
33+
34+
#include "Contact.h"
3135

3236
#include "Utilities.h"
3337

@@ -190,6 +194,13 @@ class VTK_EXPORT vtkPolyDataBooleanFilter : public vtkPolyDataAlgorithm {
190194

191195
int OperMode;
192196

197+
vtkMatrix4x4 *matrices[2];
198+
vtkLinearTransform *transforms[2];
199+
200+
vtkSmartPointer<vtkPolyData> cleanA, cleanB;
201+
202+
std::shared_ptr<Contact> contact;
203+
193204
public:
194205
vtkTypeMacro(vtkPolyDataBooleanFilter, vtkPolyDataAlgorithm);
195206
static vtkPolyDataBooleanFilter* New ();
@@ -203,6 +214,9 @@ class VTK_EXPORT vtkPolyDataBooleanFilter : public vtkPolyDataAlgorithm {
203214
void SetOperModeToDifference () { OperMode = OPER_DIFFERENCE; Modified(); }
204215
void SetOperModeToDifference2 () { OperMode = OPER_DIFFERENCE2; Modified(); }
205216

217+
void SetMatrix (int i, vtkMatrix4x4 *matrix);
218+
vtkMatrix4x4* GetMatrix (int i);
219+
206220
protected:
207221
vtkPolyDataBooleanFilter ();
208222
~vtkPolyDataBooleanFilter ();

0 commit comments

Comments
 (0)