2007-03-08 04:03:40 +08:00
|
|
|
/**********************************************************************
|
|
|
|
* File: points.h (Formerly coords.h)
|
|
|
|
* Description: Coordinate class definitions.
|
|
|
|
* Author: Ray Smith
|
|
|
|
* Created: Fri Mar 15 08:32:45 GMT 1991
|
|
|
|
*
|
|
|
|
* (C) Copyright 1991, Hewlett-Packard Ltd.
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#ifndef POINTS_H
|
|
|
|
#define POINTS_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include "elst.h"
|
|
|
|
//#include "ipeerr.h"
|
|
|
|
|
|
|
|
class FCOORD;
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///integer coordinate
|
|
|
|
class DLLSYM ICOORD
|
2007-03-08 04:03:40 +08:00
|
|
|
{
|
|
|
|
friend class FCOORD;
|
|
|
|
|
|
|
|
public:
|
2010-07-28 08:38:09 +08:00
|
|
|
///empty constructor
|
|
|
|
ICOORD() {
|
2007-03-08 04:03:40 +08:00
|
|
|
xcoord = ycoord = 0; //default zero
|
|
|
|
}
|
2010-07-28 08:38:09 +08:00
|
|
|
///constructor
|
|
|
|
///@param xin x value
|
|
|
|
///@param yin y value
|
|
|
|
ICOORD(inT16 xin,
|
|
|
|
inT16 yin) {
|
2007-03-08 04:03:40 +08:00
|
|
|
xcoord = xin;
|
|
|
|
ycoord = yin;
|
|
|
|
}
|
2010-07-28 08:38:09 +08:00
|
|
|
///destructor
|
|
|
|
~ICOORD () {
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///access function
|
2008-04-22 08:41:37 +08:00
|
|
|
NEWDELETE2 (ICOORD) inT16 x () const
|
2007-03-08 04:03:40 +08:00
|
|
|
{
|
|
|
|
return xcoord;
|
|
|
|
}
|
2010-07-28 08:38:09 +08:00
|
|
|
///access_function
|
|
|
|
inT16 y() const {
|
2007-03-08 04:03:40 +08:00
|
|
|
return ycoord;
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///rewrite function
|
|
|
|
void set_x(inT16 xin) {
|
2007-03-08 04:03:40 +08:00
|
|
|
xcoord = xin; //write new value
|
|
|
|
}
|
2010-07-28 08:38:09 +08:00
|
|
|
///rewrite function
|
|
|
|
void set_y(inT16 yin) { //value to set
|
2007-03-08 04:03:40 +08:00
|
|
|
ycoord = yin;
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
/// Set from the given x,y, shrinking the vector to fit if needed.
|
2009-07-11 10:14:57 +08:00
|
|
|
void set_with_shrink(int x, int y);
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///find sq length
|
|
|
|
float sqlength() const {
|
2007-03-08 04:03:40 +08:00
|
|
|
return (float) (xcoord * xcoord + ycoord * ycoord);
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///find length
|
|
|
|
float length() const {
|
2007-03-08 04:03:40 +08:00
|
|
|
return (float) sqrt (sqlength ());
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///sq dist between pts
|
|
|
|
float pt_to_pt_sqdist(const ICOORD &pt) const {
|
2007-03-08 04:03:40 +08:00
|
|
|
ICOORD gap;
|
|
|
|
|
|
|
|
gap.xcoord = xcoord - pt.xcoord;
|
|
|
|
gap.ycoord = ycoord - pt.ycoord;
|
|
|
|
return gap.sqlength ();
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///Distance between pts
|
|
|
|
float pt_to_pt_dist(const ICOORD &pt) const {
|
2007-03-08 04:03:40 +08:00
|
|
|
return (float) sqrt (pt_to_pt_sqdist (pt));
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///find angle
|
|
|
|
float angle() const {
|
2007-03-08 04:03:40 +08:00
|
|
|
return (float) atan2 ((double) ycoord, (double) xcoord);
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///test equality
|
|
|
|
BOOL8 operator== (const ICOORD & other) {
|
2007-03-08 04:03:40 +08:00
|
|
|
return xcoord == other.xcoord && ycoord == other.ycoord;
|
|
|
|
}
|
2010-07-28 08:38:09 +08:00
|
|
|
///test inequality
|
|
|
|
BOOL8 operator!= (const ICOORD & other) {
|
2007-03-08 04:03:40 +08:00
|
|
|
return xcoord != other.xcoord || ycoord != other.ycoord;
|
|
|
|
}
|
2010-07-28 08:38:09 +08:00
|
|
|
///rotate 90 deg anti
|
|
|
|
friend ICOORD operator! (const ICOORD &);
|
|
|
|
///unary minus
|
|
|
|
friend ICOORD operator- (const ICOORD &);
|
|
|
|
///add
|
|
|
|
friend ICOORD operator+ (const ICOORD &, const ICOORD &);
|
|
|
|
///add
|
|
|
|
friend ICOORD & operator+= (ICOORD &, const ICOORD &);
|
|
|
|
///subtract
|
|
|
|
friend ICOORD operator- (const ICOORD &, const ICOORD &);
|
|
|
|
///subtract
|
|
|
|
friend ICOORD & operator-= (ICOORD &, const ICOORD &);
|
|
|
|
///scalar product
|
|
|
|
friend inT32 operator% (const ICOORD &, const ICOORD &);
|
|
|
|
///cross product
|
|
|
|
friend inT32 operator *(const ICOORD &,
|
2007-03-08 04:03:40 +08:00
|
|
|
const ICOORD &);
|
2010-07-28 08:38:09 +08:00
|
|
|
///multiply
|
|
|
|
friend ICOORD operator *(const ICOORD &,
|
2008-04-22 08:41:37 +08:00
|
|
|
inT16);
|
2010-07-28 08:38:09 +08:00
|
|
|
///multiply
|
|
|
|
friend ICOORD operator *(inT16,
|
2007-03-08 04:03:40 +08:00
|
|
|
const ICOORD &);
|
2010-07-28 08:38:09 +08:00
|
|
|
///multiply
|
|
|
|
friend ICOORD & operator*= (ICOORD &, inT16);
|
|
|
|
///divide
|
|
|
|
friend ICOORD operator/ (const ICOORD &, inT16);
|
|
|
|
///divide
|
2008-04-22 08:41:37 +08:00
|
|
|
friend ICOORD & operator/= (ICOORD &, inT16);
|
2010-07-28 08:38:09 +08:00
|
|
|
///rotate
|
|
|
|
///@param vec by vector
|
|
|
|
void rotate(const FCOORD& vec);
|
|
|
|
|
|
|
|
/// Setup for iterating over the pixels in a vector by the well-known
|
|
|
|
/// Bresenham rendering algorithm.
|
|
|
|
/// Starting with major/2 in the accumulator, on each step move by
|
|
|
|
/// major_step, and then add minor to the accumulator. When
|
|
|
|
/// accumulator >= major subtract major and also move by minor_step.
|
2008-12-31 02:15:44 +08:00
|
|
|
void setup_render(ICOORD* major_step, ICOORD* minor_step,
|
2009-07-11 10:14:57 +08:00
|
|
|
int* major, int* minor) const;
|
2008-12-31 02:15:44 +08:00
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///serialise to ascii
|
|
|
|
void serialise_asc(FILE *f);
|
|
|
|
///serialise from ascii
|
|
|
|
void de_serialise_asc(FILE *f);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
protected:
|
2010-07-28 08:38:09 +08:00
|
|
|
inT16 xcoord; //< x value
|
|
|
|
inT16 ycoord; //< y value
|
2007-03-08 04:03:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class DLLSYM ICOORDELT:public ELIST_LINK, public ICOORD
|
|
|
|
//embedded coord list
|
|
|
|
{
|
|
|
|
public:
|
2010-07-28 08:38:09 +08:00
|
|
|
///empty constructor
|
|
|
|
ICOORDELT() {
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2010-07-28 08:38:09 +08:00
|
|
|
///constructor from ICOORD
|
|
|
|
ICOORDELT (ICOORD icoord):ICOORD (icoord) {
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2010-07-28 08:38:09 +08:00
|
|
|
///constructor
|
|
|
|
///@param xin x value
|
|
|
|
///@param yin y value
|
|
|
|
ICOORDELT(inT16 xin,
|
|
|
|
inT16 yin) {
|
2007-03-08 04:03:40 +08:00
|
|
|
xcoord = xin;
|
|
|
|
ycoord = yin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note that prep_serialise() dump() and de_dump() dont need to do anything
|
|
|
|
more than terminate recursion. */
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///set ptrs to counts
|
|
|
|
void prep_serialise() const {
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///write external bits
|
|
|
|
void dump(FILE *) const {
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///read external bits
|
|
|
|
void de_dump(FILE *) {
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///serialise to ascii
|
2008-12-31 02:15:44 +08:00
|
|
|
make_serialise(ICOORDELT)
|
|
|
|
|
|
|
|
static ICOORDELT* deep_copy(const ICOORDELT* src) {
|
|
|
|
ICOORDELT* elt = new ICOORDELT;
|
|
|
|
*elt = *src;
|
|
|
|
return elt;
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///serialise to ascii
|
2008-12-31 02:15:44 +08:00
|
|
|
void serialise_asc(FILE * f);
|
2010-07-28 08:38:09 +08:00
|
|
|
///deserialise from ascii
|
|
|
|
void de_serialise_asc(FILE *f);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
ELISTIZEH_S (ICOORDELT)
|
|
|
|
class DLLSYM FCOORD
|
|
|
|
{
|
|
|
|
public:
|
2010-07-28 08:38:09 +08:00
|
|
|
///empty constructor
|
2008-04-22 08:41:37 +08:00
|
|
|
FCOORD() {
|
2010-07-28 08:38:09 +08:00
|
|
|
}
|
|
|
|
///constructor
|
|
|
|
///@param xvalue x value
|
|
|
|
///@param yvalue y value
|
|
|
|
FCOORD(float xvalue,
|
2007-03-08 04:03:40 +08:00
|
|
|
float yvalue) {
|
|
|
|
xcoord = xvalue; //set coords
|
|
|
|
ycoord = yvalue;
|
|
|
|
}
|
|
|
|
FCOORD( //make from ICOORD
|
|
|
|
ICOORD icoord) { //coords to set
|
|
|
|
xcoord = icoord.xcoord;
|
|
|
|
ycoord = icoord.ycoord;
|
|
|
|
}
|
|
|
|
|
|
|
|
float x() const { //get coords
|
|
|
|
return xcoord;
|
|
|
|
}
|
2008-04-22 08:41:37 +08:00
|
|
|
float y() const {
|
2007-03-08 04:03:40 +08:00
|
|
|
return ycoord;
|
|
|
|
}
|
2010-07-28 08:38:09 +08:00
|
|
|
///rewrite function
|
|
|
|
void set_x(float xin) {
|
2007-03-08 04:03:40 +08:00
|
|
|
xcoord = xin; //write new value
|
|
|
|
}
|
2010-07-28 08:38:09 +08:00
|
|
|
///rewrite function
|
|
|
|
void set_y(float yin) { //value to set
|
2007-03-08 04:03:40 +08:00
|
|
|
ycoord = yin;
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///find sq length
|
|
|
|
float sqlength() const {
|
2007-03-08 04:03:40 +08:00
|
|
|
return xcoord * xcoord + ycoord * ycoord;
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///find length
|
|
|
|
float length() const {
|
2007-03-08 04:03:40 +08:00
|
|
|
return (float) sqrt (sqlength ());
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///sq dist between pts
|
|
|
|
float pt_to_pt_sqdist(const FCOORD &pt) const {
|
2007-03-08 04:03:40 +08:00
|
|
|
FCOORD gap;
|
|
|
|
|
|
|
|
gap.xcoord = xcoord - pt.xcoord;
|
|
|
|
gap.ycoord = ycoord - pt.ycoord;
|
|
|
|
return gap.sqlength ();
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///Distance between pts
|
|
|
|
float pt_to_pt_dist(const FCOORD &pt) const {
|
2007-03-08 04:03:40 +08:00
|
|
|
return (float) sqrt (pt_to_pt_sqdist (pt));
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///find angle
|
|
|
|
float angle() const {
|
2007-03-08 04:03:40 +08:00
|
|
|
return (float) atan2 (ycoord, xcoord);
|
|
|
|
}
|
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///Convert to unit vec
|
|
|
|
bool normalise();
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-07-28 08:38:09 +08:00
|
|
|
///test equality
|
|
|
|
BOOL8 operator== (const FCOORD & other) {
|
2007-03-08 04:03:40 +08:00
|
|
|
return xcoord == other.xcoord && ycoord == other.ycoord;
|
|
|
|
}
|
2010-07-28 08:38:09 +08:00
|
|
|
///test inequality
|
|
|
|
BOOL8 operator!= (const FCOORD & other) {
|
2007-03-08 04:03:40 +08:00
|
|
|
return xcoord != other.xcoord || ycoord != other.ycoord;
|
|
|
|
}
|
2010-07-28 08:38:09 +08:00
|
|
|
///rotate 90 deg anti
|
2007-03-08 04:03:40 +08:00
|
|
|
friend FCOORD operator! (const FCOORD &);
|
2010-07-28 08:38:09 +08:00
|
|
|
///unary minus
|
2007-03-08 04:03:40 +08:00
|
|
|
friend FCOORD operator- (const FCOORD &);
|
2010-07-28 08:38:09 +08:00
|
|
|
///add
|
2007-03-08 04:03:40 +08:00
|
|
|
friend FCOORD operator+ (const FCOORD &, const FCOORD &);
|
2010-07-28 08:38:09 +08:00
|
|
|
///add
|
2007-03-08 04:03:40 +08:00
|
|
|
friend FCOORD & operator+= (FCOORD &, const FCOORD &);
|
2010-07-28 08:38:09 +08:00
|
|
|
///subtract
|
2007-03-08 04:03:40 +08:00
|
|
|
friend FCOORD operator- (const FCOORD &, const FCOORD &);
|
2010-07-28 08:38:09 +08:00
|
|
|
///subtract
|
2007-03-08 04:03:40 +08:00
|
|
|
friend FCOORD & operator-= (FCOORD &, const FCOORD &);
|
2010-07-28 08:38:09 +08:00
|
|
|
///scalar product
|
2007-03-08 04:03:40 +08:00
|
|
|
friend float operator% (const FCOORD &, const FCOORD &);
|
2010-07-28 08:38:09 +08:00
|
|
|
///cross product
|
2008-04-22 08:41:37 +08:00
|
|
|
friend float operator *(const FCOORD &, const FCOORD &);
|
2010-07-28 08:38:09 +08:00
|
|
|
///multiply
|
|
|
|
friend FCOORD operator *(const FCOORD &, float);
|
|
|
|
///multiply
|
2008-04-22 08:41:37 +08:00
|
|
|
friend FCOORD operator *(float, const FCOORD &);
|
2010-07-28 08:38:09 +08:00
|
|
|
|
|
|
|
///multiply
|
2007-03-08 04:03:40 +08:00
|
|
|
friend FCOORD & operator*= (FCOORD &, float);
|
2010-07-28 08:38:09 +08:00
|
|
|
///divide
|
2007-03-08 04:03:40 +08:00
|
|
|
friend FCOORD operator/ (const FCOORD &, float);
|
2010-07-28 08:38:09 +08:00
|
|
|
///rotate
|
|
|
|
///@param vec by vector
|
|
|
|
void rotate(const FCOORD vec);
|
|
|
|
///divide
|
2007-03-08 04:03:40 +08:00
|
|
|
friend FCOORD & operator/= (FCOORD &, float);
|
|
|
|
|
|
|
|
private:
|
|
|
|
float xcoord; //2 floating coords
|
|
|
|
float ycoord;
|
|
|
|
};
|
|
|
|
|
|
|
|
#include "ipoints.h" /*do inline funcs */
|
|
|
|
#endif
|