-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPointSet.java
154 lines (127 loc) · 3.68 KB
/
PointSet.java
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
import Jcg.geometry.PointCloud_3;
import Jcg.geometry.Point_3;
import java.util.*;
/**
* Define methods for manipulating a point cloud in 3D
*
* @author Luca Castelli Aleardi
*/
public class PointSet extends PointCloud_3 {
public PointSet() {
super();
}
/**
* Load a point cloud from a text file
*/
public PointSet(String filename) {
super();
System.out.print("Creating a point cloud (from OFF file): ");
System.out.println(filename);
double x, y, z;
Jcg.io.IO.readTextFile(filename);
String line;
line=Jcg.io.IO.readLine(); // first line is empty
line=Jcg.io.IO.readLine();
String[] w=Jcg.io.IO.wordsFromString(line);
int sizePoints=Integer.parseInt(w[0]);
int i=0;
Point_3 point;
System.out.print("\tReading coordinates...");
while(i<sizePoints) {
line=Jcg.io.IO.readLine();
w = Jcg.io.IO.wordsFromString(line);
if(w!=null && w.length>0 && w[0].charAt(0)!='#') { // skip empty lines an comments
x=(new Double(w[0])).doubleValue();
y=(new Double(w[1])).doubleValue();
z=(new Double(w[2])).doubleValue();
point=new Point_3(x,y,z);
this.add(point);
i++;
}
}
System.out.println("done "+sizePoints+" points");
Jcg.io.IO.readStandardInput();
}
/**
* Return an array containing all points in the point cloud
*/
public Point_3[] toArray() {
Point_3[] tab = new Point_3[points.size()];
int i=0;
for(Point_3 p : this.points) {
tab[i] = p;
i++;
}
return tab;
}
/**
* Export the point cloud to a text file
*/
public void toFile(String filename) {
System.out.print("Exporting point set to OFF file...");
Jcg.io.IO.writeNewTextFile(filename);
int nVertices=this.size();
int nFaces=0;
// file header
Jcg.io.IO.println("OFF");
Jcg.io.IO.println(nVertices+" "+nFaces+" "+0);
// writing point coordinates
for(Point_3 p: this.listOfPoints()) { // iterate over all points
double x=p.getX().doubleValue();
double y=p.getY().doubleValue();
double z=p.getZ().doubleValue();
Jcg.io.IO.println(""+x+" "+y+" "+z);
}
Jcg.io.IO.writeStandardOutput();
System.out.println("done");
}
/**
* Return the centroid of the point cloud
*/
public Point_3 getBarycenter() {
Point_3 res = new Point_3(0, 0, 0);
for(Point_3 p : this.listOfPoints())
{
res.x += p.x;
res.y += p.y;
res.z += p.z;
}
res.multiply(1/(double)this.size());
return res;
}
/**
* Return the closest point to q, among the points of the point cloud
* <p>
* Warning: naive method, based on a linear scan
*
* @param q point query
*/
public Point_3 getClosestPoint(Point_3 q) {
//throw new Error("To be completed (TD6)");
int n = points.size();
int closest = 0;
double distMin = (double) q.squareDistance(points.get(0));
int i=0;
for (Point_3 p: this.points) {
double dist = (double) q.squareDistance(p);
if ( dist < distMin) {
closest = i;
distMin = dist;
}
i++;
}
return points.get(closest);
}
/**
* Compute and return the maximal distance from origin
*/
public double getMaxDistanceFromOrigin() {
double maxDistance=0.;
Point_3 origin=new Point_3(0., 0., 0.);
for(Point_3 p: this.points) {
double distance=Math.sqrt(p.squareDistance(origin).doubleValue());
maxDistance=Math.max(maxDistance, distance);
}
return maxDistance;
}
}