summaryrefslogtreecommitdiffstats
path: root/java/rasj/RasPoint.java
blob: b0f3793de6ba54419d8a5b71e815a96e47667201 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package rasj;

import java.lang.*;
import java.util.*;

/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/** ***********************************************************
 * <pre>
 *
 * PURPOSE:
 * This class represents an n-dimensional point vector.
 * @version $Revision: 1.9 $
 *
 *
 *
 * COMMENTS:
 *
 * </pre>
 *********************************************************** */


public class RasPoint
{
    static final String rcsid = "@(#)Package rasj, class RasPoint: $Header: /home/rasdev/CVS-repository/rasdaman/java/rasj/RasPoint.java,v 1.9 2003/12/10 21:04:23 rasdev Exp $";

  // array holding the point coordinates
    private long[] points;
    // dimensionality of the point
    private int dimensionality;
    // number of components initialized already
    private int streamInitCnt;

    /**
     * Constructor getting the dimensionality for stream initialization.
     * @param dim the dimensionality of this point
     **/
    public RasPoint(int dim)
    {
	dimensionality = dim;
	streamInitCnt = 0;

	points = new long[dimensionality];

	for(int i=0; i< dimensionality; i++)
	    points[i] = 0;
    }

    /**
     * Method for stream initialization of this point.
     * @param newElement a new dimension that is added to this point
     **/
    public RasPoint stream(long newElement) throws RasStreamInputOverflowException
    {
	if(streamInitCnt >= dimensionality)
	    throw new RasStreamInputOverflowException();

	points[streamInitCnt++] = newElement;
	return this;
    }

    /**
     * constructor taking a string representation for this point (for example "[1, 2, 3]").
     * @param stringRep the string representation for this point
     **/
    public RasPoint(String stringRep)
    {
	dimensionality = 1;
	streamInitCnt = 0;

	if(stringRep.trim().charAt(0) != '[')
	    {
		// error
		dimensionality = 0;
		return;
	    }

	// for parsing the string
	StringTokenizer strTok = new StringTokenizer(stringRep.trim(), "[,]");
	String strCurTok = null;

	// calculate dimensionality
	dimensionality = strTok.countTokens();

	points = new long[dimensionality];

	for(int i=0; i<dimensionality; i++)
	    {
		strCurTok = strTok.nextToken();
		points[i] = Long.parseLong(strCurTok.trim());
	    }
    }

    /**
     * Easy-to-use constructor for two dimensional points.
     * @param p1 the value of the first dimension
     * @param p2 the value of the second dimension
     **/
    public RasPoint(long p1, long p2)
    {
	dimensionality = 2;
	streamInitCnt = 2;

	points    = new long[dimensionality];
	points[0] = p1;
	points[1] = p2;
    }

    /**
     * Easy-to-use constructor for three dimensional points.
     * @param p1 the value of the first dimension
     * @param p2 the value of the second dimension
     * @param p3 the value of the third dimension
     **/
    public RasPoint(long p1, long p2, long p3)
    {
	dimensionality =3;
	streamInitCnt = 3;

	points    = new long[dimensionality];
	points[0] = p1;
	points[1] = p2;
	points[2] = p3;
    }

    /**
     * Easy-to-use constructor for four dimensional points.
     * @param p1 the value of the first dimension
     * @param p2 the value of the second dimension
     * @param p3 the value of the third dimension
     * @param p4 the value of the fourth dimension
     **/
    public RasPoint(long p1, long p2, long p3, long p4)
    {
	dimensionality = 4;
	streamInitCnt = 4;

	points    = new long[dimensionality];
	points[0] = p1;
	points[1] = p2;
	points[2] = p3;
	points[3] = p4;
    }

    /**
     * Easy-to-use constructor for five dimensional points.
     * @param p1 the value of the first dimension
     * @param p2 the value of the second dimension
     * @param p3 the value of the third dimension
     * @param p4 the value of the fourth dimension
     * @param p5 the value of the fifth dimension
     **/
    public RasPoint(long p1, long p2, long p3, long p4, long p5)
    {
	dimensionality = 5;
	streamInitCnt = 5;

	points    = new long[dimensionality];
	points[0] = p1;
	points[1] = p2;
	points[2] = p3;
	points[3] = p4;
	points[4] = p5;
    }

    /**
     * Default constructor.
     **/
    public RasPoint()
    {
	dimensionality = 0;
	streamInitCnt = 0;

	points = null;
    }

    /**
     * Copy constructor that initializes this point with the values of the given one.
     * @param pt the point used to copy the values from
     **/
    public RasPoint(final RasPoint pt)
    {
	dimensionality = pt.dimension();
	streamInitCnt  = dimensionality;
	points         = new long[ dimensionality ];

	try {
	    for(int i=0; i<dimensionality; i++)
		points[i] = pt.item(i);
	}
	catch(RasIndexOutOfBoundsException e) {
	    // this cannot occur (theoretically)
	    System.out.println("Error in RasPoint copy constructor.");
	}
    }

    /**
     * Read access to the i-th coordinate.
     * @param i the dimension that is to be read
     * @return the value of the i-th coordinate
     **/
    public long item(int i) throws RasIndexOutOfBoundsException
    {
	if(i < 0 || i >= dimensionality)
	    throw new RasIndexOutOfBoundsException(0, dimensionality-1, i);

	return points[i];
    }

    /**
     * write access to the i-th coordinate
     * @param i the coordinate that is to be accessed
     * @param value the value that is to be assigned to the specified coordinate
     **/
    public void setItem(int i, long value) throws RasIndexOutOfBoundsException
    {
	if(i < 0 || i >= dimensionality)
	    throw new RasIndexOutOfBoundsException(0, dimensionality-1, i);
	points[i] = value;
    }

    /**
     * This method copies the values of the given point to the current point. All
     * previously defined values of the current point will be deleted.
     * @param pt the point to be copied
     * @return the current point with its new values
     **/
    public final RasPoint setTo(final RasPoint pt) throws RasIndexOutOfBoundsException
    {
	if(this != pt)
	    {
		if((points != null) && dimensionality != pt.dimension())
		    {
			points = null;
		    }

		dimensionality = pt.dimension();
		streamInitCnt  = dimensionality;

		if(points == null)
		    points = new long[ dimensionality ];

		for(int i=0; i<dimensionality; i++)
		    points[i] = pt.item(i);

	    }

	return this;
    }

    /**
     * Compares this point to another point.
     * @param p the point to be compared
     * @return
     * <TABLE BORDER=0 CELLPADDING=3>
     * <TR><TD ALIGN=RIGHT VALIGN=TOP><B>-2</B></TD><TD> if the points have not the same dimensionality</TD></TR>
     * <TR><TD ALIGN=RIGHT VALIGN=TOP><B>-1</B></TD><TD> if the current point is "lesser" than point p.
     * <BR>In this context, "lesser" refers to the comparison of the coordinates in decreasing order 
     * of magnitude For example, the point (2,2,9) is "lesser" than the point (2,3,1).</TD></TR>
     * <TR><TD ALIGN=RIGHT VALIGN=TOP><B>0</B></TD><TD> if the two points have an equal value</TD></TR>
     * <TR><TD ALIGN=RIGHT VALIGN=TOP><B>1</B></TD><TD> if the current point is "greater" than point p
     * ("greater" is the opposite of "lesser").</TD></TR>
     * </TABLE>
     **/
    public final int comparedWith(final  RasPoint p)
    {
	if(dimensionality != p.dimensionality)
	    return -2;

	try {
	    for (int i = 0; i < dimensionality; i++)
		{
		    if (points[i] > p.item(i))
			return 1;
		    if (points[i] < p.item(i))
			return -1 ;
		}
	    return 0;
	}
	catch(RasIndexOutOfBoundsException e) {
	    // this cannot occur (theoretically)
	    System.out.println("Error in method RasPoint.comparedWith().");
	    return -2;
	}
    }

    /**
     * Method for testing equality of two points.<BR>Two points are equal 
     * if they have the same dimensionality and identic values in each dimension.
     * @param pt the point that is compared to the current point
     * @return true if the two points are equal
     **/
    public boolean equals(final RasPoint pt) throws RasDimensionMismatchException
    {
	boolean returnValue = false;

	if(dimensionality != pt.dimensionality)
        {
            throw new RasDimensionMismatchException(dimensionality, pt.dimensionality);
        }
	try {
	    returnValue = true;
	    for(int i=0; i<dimensionality && returnValue ; i++)
		returnValue &= (points[i] == pt.item(i));
	    return returnValue;
	}
	catch(RasIndexOutOfBoundsException e) {
	    // this cannot occur (theoretically)
	    return false;
	}
    }

    /**
     * Method for testing inequality. This is the negation of the equals method.
     * @param pt the point that is compared to the current point
     * @return true if the two points are not equal
     **/
    public boolean notEquals(final RasPoint pt) throws RasDimensionMismatchException
    {
	return !equals(pt);
    }

    /**
     * Method for vector addition. The current point will be modified to contain
     * the result of the addition.
     * @param pt the point ot be added to the current point
     * @return the current point after the addition
     **/
    public RasPoint add(final RasPoint pt) throws RasDimensionMismatchException
    {
	if(dimensionality != pt.dimension())
	    throw new RasDimensionMismatchException(dimensionality, pt.dimension());

	try {
	    RasPoint result = new RasPoint(dimensionality);
	    for(int i=0; i<dimensionality; i++)
		{
		    result.stream(points[i] + pt.item(i));
		}
	    return result;
	}
	catch(RasException e) {
	    // this cannot occur (theoretically)
	    System.err.println("Error in method RasPoint.add().");
	    return null;
	}
    }


    /**
     * Method for vector multiplication. The current point will be modified to contain
     * the result of the multiplication.
     * @param pt the point ot be multiplied to the current point
     * @return the current point after the multiplication
     **/
    public RasPoint mult(final RasPoint pt) throws RasDimensionMismatchException
    {
	if(dimensionality != pt.dimension())
	    throw new RasDimensionMismatchException(dimensionality, pt.dimension());

	try {
	    RasPoint result= new RasPoint(dimensionality);
	    for(int i=0; i<dimensionality; i++)
		{
		    result.stream(points[i] * pt.item(i));
		}
	    return result;
	}
	catch(RasException e) {
	    // this cannot occur (theoretically)
	    System.err.println("Error in method RasPoint.mult().");
	    return null;
	}
    }


    /**
     * Gets the dimensionality of this point.
     * @return the dimensionality of this point
     **/
    public int dimension()
    {
	return dimensionality;
    }


    /**
     * Gives back the string representation of this point.
     * @return the string representation of this point
     **/
    public String toString()
    {
	String retString = "";
	if(dimensionality > 0)
	    {
		for(int i=0; i<dimensionality-1; i++)
		    {
			retString = retString + Long.toString(points[i]) + ",";
		    }
		retString = retString + Long.toString(points[dimensionality-1]);
	    }

	return "[" + retString  + "]";
    }

}