GridTopology.cpp 7.84 KB
Newer Older
promayon's avatar
promayon committed
1
/*****************************************************************************
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2012 UJF-Grenoble 1, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
 *
 * Visit http://camitk.imag.fr for more information
 *
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * CamiTK 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 version 3 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $CAMITK_LICENCE_END$
 ****************************************************************************/
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
 * @file GridTopology.cpp
 * @brief Class implementation for 3D grid mesh creation
 * @author Mathieu BAILET
 * @date July 5 2012
 */

#include "GridTopology.h"
#include <MeshComponent.h>
#include <Log.h>
#include <Application.h>
#include <MainWindow.h>

#include <vtkCell.h>
#include <vtkPoints.h>
#include <vtkSmartPointer.h>
#include <vtkHexahedron.h>
42
#include <vtkLine.h>
43
44
45
46
47
48
49
50
51
52
#include <vtkCellArray.h>

GridTopology::GridTopology(ActionExtension *extension) : Action(extension) {
    setName("Grid Topology");
    setDescription("Build a regular grid");
    setComponent(""); // empty component: it does not need any data to be triggered
    setFamily("Mesh Processing");
    addTag("Build Grid");

    setProperty("Tetrahedralize", false);
53
    setProperty("Network", false);
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
    setProperty("Dimension X", 1);
    setProperty("Dimension Y", 1);
    setProperty("Dimension Z", 1);
    setProperty("Position X", 0.);
    setProperty("Position Y", 0.);
    setProperty("Position Z", 0.);
    setProperty("Width", 1.);
    setProperty("Height", 1.);
    setProperty("Depth", 1.);
}

GridTopology::~GridTopology() {
}

Action::ApplyStatus GridTopology::apply() {
    int dimX = property("Dimension X").toInt();
    int dimY = property("Dimension Y").toInt();
    int dimZ = property("Dimension Z").toInt();
    double posX = property("Position X").toDouble();
    double posY = property("Position Y").toDouble();
    double posZ = property("Position Z").toDouble();
    double width = property("Width").toDouble();
    double height = property("Height").toDouble();
    double depth = property("Depth").toDouble();
    double stepX = width/dimX;
    double stepY = height/dimY;
    double stepZ = depth/dimZ;

    double pt[3];

    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();

    for (int i=0; i<=dimX; i++)
        for (int j=0; j<=dimY; j++)
            for (int k=0; k<=dimZ; k++) {
                pt[0] = posX + i * stepX;
                pt[1] = posY + j * stepY;
                pt[2] = posZ + k * stepZ;
                points->InsertNextPoint(pt);
            }

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    vtkSmartPointer<vtkUnstructuredGrid> grid = vtkSmartPointer<vtkUnstructuredGrid>::New();
    grid->SetPoints(points);


    if (property("Network").toBool()) {
        buildNetwork(grid);
    }
    else {
        buildGrid(grid);

    }

    grid->Update();

    MeshComponent* result = new MeshComponent(grid, "Grid_" + QString::number(dimX)+"x"+QString::number(dimY)+"x"+QString::number(dimZ));
    Application::refresh();
    Application::resetProgressBar();
    Application::showStatusBarMessage("");

    return SUCCESS;
}

void GridTopology::buildGrid(vtkSmartPointer<vtkUnstructuredGrid> grid) {
118
    vtkSmartPointer<vtkCellArray> elements = vtkSmartPointer<vtkCellArray>::New();
119
120
121
122
123
    vtkSmartPointer<vtkHexahedron> element = vtkSmartPointer<vtkHexahedron>::New();

    int dimX = property("Dimension X").toInt();
    int dimY = property("Dimension Y").toInt();
    int dimZ = property("Dimension Z").toInt();
124
125
126
127

    for (int i=1; i<=dimX; i++)
        for (int j=1; j<=dimY; j++)
            for (int k=1; k<=dimZ; k++) {
128
                
129
130
131
132
133
134
135
136
137
138
139
140
141

                element->GetPointIds()->SetId (0, (((i-1)*(dimY+1))+j-1)*(dimZ+1)+k-1);
                element->GetPointIds()->SetId (1, (((i-1)*(dimY+1))+j)*(dimZ+1)+k-1);
                element->GetPointIds()->SetId (2, ((i*(dimY+1))+j)*(dimZ+1)+k-1);
                element->GetPointIds()->SetId (3, ((i*(dimY+1))+(j-1))*(dimZ+1)+k-1);
                element->GetPointIds()->SetId (4, (((i-1)*(dimY+1))+j-1)*(dimZ+1)+k);
                element->GetPointIds()->SetId (5, (((i-1)*(dimY+1))+j)*(dimZ+1)+k);
                element->GetPointIds()->SetId (6, ((i*(dimY+1))+j)*(dimZ+1)+k);
                element->GetPointIds()->SetId (7, ((i*(dimY+1))+(j-1))*(dimZ+1)+k);

                elements->InsertNextCell ( element );
            }

142
    //grid->Allocate(dimX*dimY*dimZ);
143
    grid->SetCells(VTK_HEXAHEDRON, elements);
144
}
145

146
147
148
void GridTopology::buildNetwork(vtkSmartPointer<vtkUnstructuredGrid> grid) {
    vtkSmartPointer<vtkCellArray> elements = vtkSmartPointer<vtkCellArray>::New();
    vtkSmartPointer<vtkLine> element = vtkSmartPointer<vtkLine>::New();
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
    int dimX = property("Dimension X").toInt();
    int dimY = property("Dimension Y").toInt();
    int dimZ = property("Dimension Z").toInt();

    for (int i=1; i<=dimX; i++)
        for (int j=0; j<=dimY; j++)
            for (int k=0; k<=dimZ; k++) {
                element->GetPointIds()->SetId (0, (((i-1)*( dimY + 1 ))+j)*( dimZ + 1 )+k);
                element->GetPointIds()->SetId (1, ((i*( dimY + 1 ))+j)*( dimZ + 1 )+k);
                elements->InsertNextCell ( element );
            }

    for (int i=0; i<=dimX; i++)
        for (int j=1; j<=dimY; j++)
            for (int k=0; k<=dimZ; k++) {
                element->GetPointIds()->SetId (0, ( ( i * ( dimY + 1 ) ) + j - 1 ) * ( dimZ + 1 ) +k );
                element->GetPointIds()->SetId (1, ( ( i * ( dimY + 1 ) ) + j ) * ( dimZ + 1 ) + k );
                elements->InsertNextCell ( element );
            }

    for (int i=0; i<=dimX; i++)
        for (int j=0; j<=dimY; j++)
            for (int k=1; k<=dimZ; k++) {
                element->GetPointIds()->SetId (0, ((i*( dimY + 1 ))+j)*( dimZ + 1 )+k-1);
                element->GetPointIds()->SetId (1, ((i*( dimY + 1 ))+j)*( dimZ + 1 )+k);
                elements->InsertNextCell ( element );
            }

    for (int i=1; i<=dimX; i++)
        for (int j=1; j<=dimY; j++)
            for (int k=1; k<=dimZ; k++) {
                element->GetPointIds()->SetId (0, (((i-1)*( dimY + 1 ))+j-1)*( dimZ + 1 )+k-1);
                element->GetPointIds()->SetId (1, ((i*( dimY + 1 ))+j)*( dimZ + 1 )+k);
                elements->InsertNextCell ( element );
            }

    for (int i=1; i<=dimX; i++)
        for (int j=1; j<=dimY; j++)
            for (int k=1; k<=dimZ; k++) {
                element->GetPointIds()->SetId (0, (((i-1)*( dimY + 1 ))+j)*( dimZ + 1 )+k);
                element->GetPointIds()->SetId (1, ((i*( dimY + 1 ))+j-1)*( dimZ + 1 )+k-1);
                elements->InsertNextCell ( element );
            }

    for (int i=1; i<=dimX; i++)
        for (int j=1; j<=dimY; j++)
            for (int k=1; k<=dimZ; k++) {
                element->GetPointIds()->SetId (0, (((i-1)*( dimY + 1 ))+j-1)*( dimZ + 1 )+k);
                element->GetPointIds()->SetId (1, ((i*( dimY + 1 ))+j)*( dimZ + 1 )+k-1);
                elements->InsertNextCell ( element );
            }

    for (int i=1; i<=dimX; i++)
        for (int j=1; j<=dimY; j++)
            for (int k=1; k<=dimZ; k++) {
                element->GetPointIds()->SetId (0, (((i-1)*( dimY + 1 ))+j)*( dimZ + 1 )+k-1);
                element->GetPointIds()->SetId (1, ((i*( dimY + 1 ))+j-1)*( dimZ + 1 )+k);
                elements->InsertNextCell ( element );
            }
            
            
            grid->SetCells(VTK_LINE, elements);
212
}