@@ -19,6 +19,7 @@ import (
19
19
20
20
"github.com/deadsy/sdfx/render"
21
21
"github.com/deadsy/sdfx/sdf"
22
+ "github.com/deadsy/sdfx/vec/conv"
22
23
)
23
24
24
25
//-----------------------------------------------------------------------------
@@ -68,7 +69,7 @@ func NewDualContouringV2(farAway float64, centerPush float64, raycastScaleAndSig
68
69
// Info returns a string describing the rendered volume.
69
70
func (dc * DualContouringV2 ) Info (s sdf.SDF3 , meshCells int ) string {
70
71
resolution , cells := dc .getCells (s , meshCells )
71
- return fmt .Sprintf ("%dx%dx%d, resolution %.2f" , cells [ 0 ] , cells [ 1 ] , cells [ 2 ] , resolution )
72
+ return fmt .Sprintf ("%dx%dx%d, resolution %.2f" , cells . X , cells . Y , cells . Z , resolution )
72
73
}
73
74
74
75
// Render produces a 3d triangle mesh over the bounding volume of an sdf3.
@@ -84,7 +85,7 @@ func (dc *DualContouringV2) Render(s sdf.SDF3, meshCells int, output chan<- *ren
84
85
func (dc * DualContouringV2 ) getCells (s sdf.SDF3 , meshCells int ) (float64 , sdf.V3i ) {
85
86
bbSize := s .BoundingBox ().Size ()
86
87
resolution := bbSize .MaxComponent () / float64 (meshCells )
87
- return resolution , bbSize .DivScalar (resolution ). ToV3i ( )
88
+ return resolution , conv . V3ToV3i ( bbSize .DivScalar (resolution ))
88
89
}
89
90
90
91
//-----------------------------------------------------------------------------
@@ -156,23 +157,23 @@ type dcVoxelInfo struct {
156
157
157
158
func (dc * DualContouringV2 ) placeVertices (s * dcSdf , cells sdf.V3i ) (buf []sdf.V3 , bufMap []* dcVoxelInfo , bufMapIndexed map [sdf.V3i ]* dcVoxelInfo ) {
158
159
// Start with big enough buffers for performance avoiding allocations (but not too big, may expand later)
159
- buf = make ([]sdf.V3 , 0 , dcMaxI (32 , cells [ 0 ] * cells [ 1 ] * cells [ 2 ] / 100 ))
160
- bufMap = make ([]* dcVoxelInfo , 0 , dcMaxI (32 , cells [ 0 ] * cells [ 1 ] * cells [ 2 ] / 100 ))
161
- bufMapIndexed = make (map [sdf.V3i ]* dcVoxelInfo , dcMaxI (32 , cells [ 0 ] * cells [ 1 ] * cells [ 2 ] / 100 ))
160
+ buf = make ([]sdf.V3 , 0 , dcMaxI (32 , cells . X * cells . Y * cells . Z / 100 ))
161
+ bufMap = make ([]* dcVoxelInfo , 0 , dcMaxI (32 , cells . X * cells . Y * cells . Z / 100 ))
162
+ bufMapIndexed = make (map [sdf.V3i ]* dcVoxelInfo , dcMaxI (32 , cells . X * cells . Y * cells . Z / 100 ))
162
163
// Other pre-allocated vertex placing buffers
163
164
normals := make ([]sdf.V3 , 0 , 11 )
164
165
planeDs := make ([]float64 , 0 , 11 )
165
166
// Some cached variables
166
167
bb := s .BoundingBox ()
167
- cellSize := bb .Size ().Div (cells . ToV3 ( ))
168
+ cellSize := bb .Size ().Div (conv . V3iToV3 ( cells ))
168
169
cellSizeHalf := cellSize .DivScalar (2 )
169
170
cellIndex := sdf.V3i {}
170
171
// Iterate over all cells (could be parallelized, synchronizing on each vertex positioned)
171
- for cellIndex [ 0 ] = 0 ; cellIndex [ 0 ] < cells [ 0 ] ; cellIndex [ 0 ] ++ {
172
- for cellIndex [ 1 ] = 0 ; cellIndex [ 1 ] < cells [ 1 ] ; cellIndex [ 1 ] ++ {
173
- for cellIndex [ 2 ] = 0 ; cellIndex [ 2 ] < cells [ 2 ] ; cellIndex [ 2 ] ++ {
172
+ for cellIndex . X = 0 ; cellIndex . X < cells . X ; cellIndex . X ++ {
173
+ for cellIndex . Y = 0 ; cellIndex . Y < cells . Y ; cellIndex . Y ++ {
174
+ for cellIndex . Z = 0 ; cellIndex . Z < cells . Z ; cellIndex . Z ++ {
174
175
// Generate each vertex (if the surface crosses the voxel)
175
- cellStart := bb .Min .Add (cellSize .Mul (cellIndex . ToV3 ( )))
176
+ cellStart := bb .Min .Add (cellSize .Mul (conv . V3iToV3 ( cellIndex )))
176
177
cellCenter := cellStart .Add (cellSizeHalf )
177
178
vertexPos := dc .placeVertex (s , cellStart , cellCenter , cellSize , normals [:0 ], planeDs [:0 ])
178
179
if ! math .IsInf (vertexPos .X , 0 ) {
@@ -203,12 +204,12 @@ func (dc *DualContouringV2) placeVertex(s *dcSdf, cellStart, cellCenter, cellSiz
203
204
204
205
//// Add candidate planes from all surface-crossing edges (using the surface point on the edge)
205
206
for _ , edge := range dcEdges { // Use edges instead of corners to generate less positions and normals.
206
- if ((inside >> edge [ 0 ] ) & 1 ) == ((inside >> edge [ 1 ] ) & 1 ) { // Not crossing edge
207
+ if ((inside >> edge . X ) & 1 ) == ((inside >> edge . Y ) & 1 ) { // Not crossing edge
207
208
continue
208
209
}
209
210
//crossingCorners = crossingCorners | (1 << edge[0]) | (1 << edge[1])
210
- cornerPos1 := cellStart .Add (dcCorners [edge [ 0 ] ].Mul (cellSize ))
211
- cornerPos2 := cellStart .Add (dcCorners [edge [ 1 ] ].Mul (cellSize ))
211
+ cornerPos1 := cellStart .Add (dcCorners [edge . X ].Mul (cellSize ))
212
+ cornerPos2 := cellStart .Add (dcCorners [edge . Y ].Mul (cellSize ))
212
213
//edgeSurfPos := dcApproximateZeroCrossingPosition(s, cornerPos1, cornerPos2)
213
214
dir := cornerPos2 .Sub (cornerPos1 )
214
215
dirLength := dir .Length ()
@@ -291,7 +292,7 @@ func (dc *DualContouringV2) generateTriangles(s *dcSdf, vertices []sdf.V3, info
291
292
// Connect to triangles in the 3 main axes (two triangles each, if crossing the surface)
292
293
for ai := 0 ; ai < 3 ; ai ++ {
293
294
edge := dcFarEdges [ai ]
294
- if ((inside >> edge [ 0 ] ) & 1 ) == ((inside >> edge [ 1 ] ) & 1 ) {
295
+ if ((inside >> edge . X ) & 1 ) == ((inside >> edge . Y ) & 1 ) {
295
296
continue // Not a crossing
296
297
}
297
298
@@ -324,7 +325,7 @@ func (dc *DualContouringV2) generateTriangles(s *dcSdf, vertices []sdf.V3, info
324
325
t1 := & render.Triangle3 {V : [3 ]sdf.V3 {vertices [v0 ], vertices [v3 .bufIndex ], vertices [v2 .bufIndex ]}}
325
326
326
327
// Get the normals right:
327
- if ((inside >> edge [ 0 ] ) & 1 ) != uint8 (ai & 1 ) { // xor
328
+ if ((inside >> edge . X ) & 1 ) != uint8 (ai & 1 ) { // xor
328
329
t0 = dcFlip (t0 )
329
330
t1 = dcFlip (t1 )
330
331
}
0 commit comments