forked from fortmeier/CUDA-FE-With-Haptics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Visualization_kernels.cu
163 lines (128 loc) · 4.41 KB
/
Visualization_kernels.cu
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
/***************************************************************************/
/* CUDA based TLED Solver */
/* {c} 2008-2010 Karsten Noe */
/* The Alexandra Institute */
/* See our blog on cg.alexandra.dk */
/***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "TetrahedralMesh.h"
#include "cutil_math.h"
//#include "float_utils.h"
using namespace TLED;
inline __host__ __device__ float3 crop_last_dim(float4 ui)
{
return make_float3( ui.x, ui.y, ui.z );
}
__global__ void
extractSurface_k(float3 *tris, Tetrahedron *tetrahedra, Point *points, unsigned int numTets)
{
int me_idx = blockIdx.x * blockDim.x + threadIdx.x;
if (me_idx>=numTets)
return;
int zeroVertices[4];
int numZeroVertices = 0;
Tetrahedron tet = tetrahedra[me_idx];
if (tet.x<0) return; // illegal tetrahedron
// if (points[tet.x].distance==0)
zeroVertices[numZeroVertices++] = tet.x;
// if (points[tet.y].distance==0)
zeroVertices[numZeroVertices++] = tet.y;
// if (points[tet.z].distance==0)
zeroVertices[numZeroVertices++] = tet.z;
// if (points[tet.w].distance==0)
zeroVertices[numZeroVertices++] = tet.w;
// printf("numZeroes: %i", numZeroVertices);
if (numZeroVertices>=3 )
{
for (int i=0; i<3; i++)
tris[(3*me_idx)+i] = crop_last_dim(points[zeroVertices[i]]);
}
else
{
for (int i=0; i<3; i++)
tris[(3*me_idx)+i] = make_float3(0,0,0);
}
}
__global__ void
extractSurfaceWithDisplacements_k(float3 *tris, Tetrahedron *tetrahedra, Point *points, float4 *displacements, unsigned int numTets)
{
int me_idx = blockIdx.x * blockDim.x + threadIdx.x;
if (me_idx>=numTets)
return;
int4 tet = tetrahedra[me_idx];
if (tet.x<0) return; // illegal tetrahedron
int zeroVertices[4];
int numZeroVertices = 0;
// if (points[tetrahedra[me_idx].x].distance==0)
zeroVertices[numZeroVertices++] = tet.x;
// if (points[tetrahedra[me_idx].y].distance==0)
zeroVertices[numZeroVertices++] = tet.y;
// if (points[tetrahedra[me_idx].z].distance==0)
zeroVertices[numZeroVertices++] = tet.z;
// if (points[tetrahedra[me_idx].w].distance==0)
zeroVertices[numZeroVertices++] = tet.w;
// printf("numZeroes: %i", numZeroVertices);
if (numZeroVertices>=3)
{
for (int i=0; i<3; i++)
{
float3 pos = crop_last_dim(points[zeroVertices[i]]);
float3 displacement = crop_last_dim(displacements[zeroVertices[i]]);
pos.x += displacement.x;
pos.y += displacement.y;
pos.z += displacement.z;
tris[(3*me_idx)+i] = pos;
}
}
else
{
for (int i=0; i<3; i++)
tris[(3*me_idx)+i] = make_float3(0,0,0);
}
}
/*
// cross product
inline __host__ __device__ float3 cross(float3 a, float3 b)
{
return make_float3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
}
*/
__device__
float3 calcNormal(float4 *v0, float4 *v1, float4 *v2)
{
float3 edge0 = crop_last_dim(*v1 - *v0);
float3 edge1 = crop_last_dim(*v2 - *v0);
// note - it's faster to perform normalization in vertex shader rather than here
return cross(edge0, edge1);
}
__global__ void
updateSurfacePositionsFromDisplacements_k(float3 *tris, float3 *normals, TriangleSurface surface, Point *points, float4 *displacements)
{
int me_idx = blockIdx.x * blockDim.x + threadIdx.x;
if (me_idx>=surface.numFaces) return;
Triangle triangle = surface.faces[me_idx];
float4 pos, pos2, pos3, displacement;
pos = points[triangle.x-1];
displacement = displacements[triangle.x-1];
pos.x += displacement.x;
pos.y += displacement.y;
pos.z += displacement.z;
tris[(3*me_idx)+0] = crop_last_dim(pos);
pos2 = points[triangle.y-1];
displacement = displacements[triangle.y-1];
pos2.x += displacement.x;
pos2.y += displacement.y;
pos2.z += displacement.z;
tris[(3*me_idx)+1] = crop_last_dim(pos2);
pos3 = points[triangle.z-1];
displacement = displacements[triangle.z-1];
pos3.x += displacement.x;
pos3.y += displacement.y;
pos3.z += displacement.z;
tris[(3*me_idx)+2] = crop_last_dim(pos3);
float3 normal = calcNormal(&pos,&pos2,&pos3);
normals[(3*me_idx)+0] = normal;
normals[(3*me_idx)+1] = normal;
normals[(3*me_idx)+2] = normal;
}