1//////////////////////////////////////////////////////////////////////
2// LibFile: debug.scad
3// Helpers to make debugging OpenScad code easier.
4// To use, add the following lines to the beginning of your file:
5// ```
6// include <BOSL/constants.scad>
7// use <BOSL/debug.scad>
8// ```
9//////////////////////////////////////////////////////////////////////
10
11/*
12BSD 2-Clause License
13
14Copyright (c) 2017, Revar Desmera
15All rights reserved.
16
17Redistribution and use in source and binary forms, with or without
18modification, are permitted provided that the following conditions are met:
19
20* Redistributions of source code must retain the above copyright notice, this
21 list of conditions and the following disclaimer.
22
23* Redistributions in binary form must reproduce the above copyright notice,
24 this list of conditions and the following disclaimer in the documentation
25 and/or other materials provided with the distribution.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37*/
38
39include <transforms.scad>
40include <math.scad>
41include <paths.scad>
42include <beziers.scad>
43
44
45// Section: Debugging Polyhedrons
46
47
48// Module: debug_vertices()
49// Description:
50// Draws all the vertices in an array, at their 3D position, numbered by their
51// position in the vertex array. Also draws any children of this module with
52// transparency.
53// Arguments:
54// vertices = Array of point vertices.
55// size = The size of the text used to label the vertices.
56// disabled = If true, don't draw numbers, and draw children without transparency. Default = false.
57// Example:
58// verts = [for (z=[-10,10], y=[-10,10], x=[-10,10]) [x,y,z]];
59// faces = [[0,1,2], [1,3,2], [0,4,5], [0,5,1], [1,5,7], [1,7,3], [3,7,6], [3,6,2], [2,6,4], [2,4,0], [4,6,7], [4,7,5]];
60// debug_vertices(vertices=verts, size=2) {
61// polyhedron(points=verts, faces=faces);
62// }
63module debug_vertices(vertices, size=1, disabled=false) {
64 if (!disabled) {
65 echo(vertices=vertices);
66 color("blue") {
67 for (i = [0:len(vertices)-1]) {
68 v = vertices[i];
69 translate(v) {
70 up(size/8) zrot($vpr[2]) xrot(90) {
71 linear_extrude(height=size/10, center=true, convexity=10) {
72 text(text=str(i), size=size, halign="center");
73 }
74 }
75 sphere(size/10);
76 }
77 }
78 }
79 }
80 if ($children > 0) {
81 if (!disabled) {
82 color([0.2, 1.0, 0, 0.5]) children();
83 } else {
84 children();
85 }
86 }
87}
88
89
90
91// Module: debug_faces()
92// Description:
93// Draws all the vertices at their 3D position, numbered in blue by their
94// position in the vertex array. Each face will have their face number drawn
95// in red, aligned with the center of face. All children of this module are drawn
96// with transparency.
97// Arguments:
98// vertices = Array of point vertices.
99// faces = Array of faces by vertex numbers.
100// size = The size of the text used to label the faces and vertices.
101// disabled = If true, don't draw numbers, and draw children without transparency. Default = false.
102// Example:
103// verts = [for (z=[-10,10], y=[-10,10], x=[-10,10]) [x,y,z]];
104// faces = [[0,1,2], [1,3,2], [0,4,5], [0,5,1], [1,5,7], [1,7,3], [3,7,6], [3,6,2], [2,6,4], [2,4,0], [4,6,7], [4,7,5]];
105// debug_faces(vertices=verts, faces=faces, size=2) {
106// polyhedron(points=verts, faces=faces);
107// }
108module debug_faces(vertices, faces, size=1, disabled=false) {
109 if (!disabled) {
110 vlen = len(vertices);
111 color("red") {
112 for (i = [0:len(faces)-1]) {
113 face = faces[i];
114 if (face[0] < 0 || face[1] < 0 || face[2] < 0 || face[0] >= vlen || face[1] >= vlen || face[2] >= vlen) {
115 echo("BAD FACE: ", vlen=vlen, face=face);
116 } else {
117 v0 = vertices[face[0]];
118 v1 = vertices[face[1]];
119 v2 = vertices[face[2]];
120 c = (v0 + v1 + v2) / 3;
121 dv0 = normalize(v1 - v0);
122 dv1 = normalize(v2 - v0);
123 nrm0 = normalize(cross(dv0, dv1));
124 nrm1 = [0, 0, 1];
125 axis = normalize(cross(nrm0, nrm1));
126 ang = vector_angle(nrm0, nrm1);
127 theta = atan2(nrm0[1], nrm0[0]);
128 translate(c) {
129 rotate(a=180-ang, v=axis) {
130 zrot(theta-90)
131 linear_extrude(height=size/10, center=true, convexity=10) {
132 union() {
133 text(text=str(i), size=size, halign="center");
134 text(text=str("_"), size=size, halign="center");
135 }
136 }
137 }
138 }
139 }
140 }
141 }
142 }
143 debug_vertices(vertices, size=size, disabled=disabled) {
144 children();
145 }
146 if (!disabled) {
147 echo(faces=faces);
148 }
149}
150
151
152
153// Module: debug_polyhedron()
154// Description:
155// A drop-in module to replace `polyhedron()` and help debug vertices and faces.
156// Draws all the vertices at their 3D position, numbered in blue by their
157// position in the vertex array. Each face will have their face number drawn
158// in red, aligned with the center of face. All given faces are drawn with
159// transparency. All children of this module are drawn with transparency.
160// Works best with Thrown-Together preview mode, to see reversed faces.
161// Arguments:
162// vertices = Array of point vertices.
163// faces = Array of faces by vertex numbers.
164// txtsize = The size of the text used to label the faces and vertices.
165// disabled = If true, act exactly like `polyhedron()`. Default = false.
166// Example:
167// verts = [for (z=[-10,10], a=[0:120:359.9]) [10*cos(a),10*sin(a),z]];
168// faces = [[0,1,2], [5,4,3], [0,3,4], [0,4,1], [1,4,5], [1,5,2], [2,5,3], [2,3,0]];
169// debug_polyhedron(points=verts, faces=faces, txtsize=1);
170module debug_polyhedron(points, faces, convexity=10, txtsize=1, disabled=false) {
171 debug_faces(vertices=points, faces=faces, size=txtsize, disabled=disabled) {
172 polyhedron(points=points, faces=faces, convexity=convexity);
173 }
174}
175
176
177
178// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap