-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_PP_room.m
More file actions
101 lines (83 loc) · 2.78 KB
/
test_PP_room.m
File metadata and controls
101 lines (83 loc) · 2.78 KB
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
clear; clc; close all;
cc = distinguishable_colors(20);
addpath("stlTools/");
addpath('path-planning/');
%% load data
path = fullfile("stl/","PP_room.stl");
[V, F, ~, ~] = stlRead(path);
room.vertices = V * 0.0254;
room.faces = F;
% stlPlot(room.vertices, room.faces, 'test')
V = V *0.0254;
% 2) pick bottom faces
zmin = min(V(:,3));
tol = 1e-6;
Z1 = V(F(:,1),3); Z2 = V(F(:,2),3); Z3 = V(F(:,3),3);
isBottom = abs(Z1-zmin)<tol & abs(Z2-zmin)<tol & abs(Z3-zmin)<tol;
bottomF = F(isBottom,:);
% isAllZero = ~any(bottomF(:));
isAllZero = 1;
if isAllZero == 0
% 3) triangulation + free boundary
TRb = triangulation(bottomF, V);
E = freeBoundary(TRb); % L×2 list of edges :contentReference[oaicite:1]{index=1}
% 4) group into loops
loops = obj.edgeLoops(E);
% 5) build P = [x y; NaN NaN; x y; NaN NaN; …]
P = NaN(0,2);
for k = 1:numel(loops)
idxs = loops{k};
coords = V(idxs,1:2); % K×2
P = [P; coords; NaN NaN]; %#ok<AGROW>
end
% 6) create a single polyshape with holes from P
obj.pg = polyshape(P); % NaN separators => holes :contentReference[oaicite:2]{index=2}
% 1) Get the NaN-delimited boundary of pg
[xb, yb] = boundary(obj.pg); % xb,yb include NaNs between loops :contentReference[oaicite:0]{index=0}
% 2) Isolate only the *first* loop (outer boundary)
nanIdx = find(isnan(xb), 1);
if isempty(nanIdx)
outerX = xb;
outerY = yb;
else
outerX = xb(1:nanIdx-1);
outerY = yb(1:nanIdx-1);
end
% 3) Delaunay-triangulate those outer points
DT = delaunayTriangulation(outerX, outerY); % :contentReference[oaicite:1]{index=1}
T = DT.ConnectivityList; % M×3 array of triangles
P = DT.Points; % Mpoints×2 coordinates
% 4) Keep only triangles whose centroids lie *inside* the outer polygon
cents = ( P(T(:,1),:) + P(T(:,2),:) + P(T(:,3),:) )/3; % M×2
in = inpolygon(cents(:,1), cents(:,2), outerX, outerY);
TF = T(in,:);
% 5) Lift to 3D at z=0
Vg = [P, zeros(size(P,1),1)]; % N×3 vertices at z=0
Fg = TF; % faces
else
% 1) Compute bounding box of the STL
xmin = min(V(:,1));
xmax = max(V(:,1));
ymin = min(V(:,2));
ymax = max(V(:,2));
% Optional: expand slightly beyond the object
pad = 0.1; % 10 cm margin
xmin = xmin - pad;
xmax = xmax + pad;
ymin = ymin - pad;
ymax = ymax + pad;
% 2) Define rectangle vertices (at z=0)
Vg = [
xmin ymin 0;
xmax ymin 0;
xmax ymax 0;
xmin ymax 0
];
% 3) Define faces (two triangles)
Fg = [
1 2 3;
1 3 4
];
end
[vv,ff] = stlAddVerts(V, F, Vg, Fg);
stlPlot(vv, ff, 'results')