2007-03-08 04:03:40 +08:00
|
|
|
/**********************************************************************
|
|
|
|
* File: polyblk.c (Formerly poly_block.c)
|
|
|
|
* Description: Polygonal blocks
|
|
|
|
* Author: Sheelagh Lloyd?
|
|
|
|
* Created:
|
|
|
|
*
|
|
|
|
* (C) Copyright 1993, 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.
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
|
2009-07-11 10:14:57 +08:00
|
|
|
#include "mfcpch.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "elst.h"
|
|
|
|
#include "polyblk.h"
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-09-30 23:53:40 +08:00
|
|
|
// Include automatically generated configuration file if running autoconf.
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config_auto.h"
|
|
|
|
#endif
|
|
|
|
|
2009-07-11 10:14:57 +08:00
|
|
|
#include "hpddef.h" // must be last (handpd.dll)
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
#define PBLOCK_LABEL_SIZE 150
|
|
|
|
#define INTERSECTING MAX_INT16
|
|
|
|
|
2008-04-22 08:41:37 +08:00
|
|
|
int lessthan(const void *first, const void *second);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2009-07-11 10:14:57 +08:00
|
|
|
POLY_BLOCK::POLY_BLOCK(ICOORDELT_LIST *points, PolyBlockType t) {
|
2007-03-08 04:03:40 +08:00
|
|
|
ICOORDELT_IT v = &vertices;
|
|
|
|
|
2009-07-11 10:14:57 +08:00
|
|
|
vertices.clear();
|
|
|
|
v.move_to_first();
|
|
|
|
v.add_list_before(points);
|
2008-04-22 08:41:37 +08:00
|
|
|
compute_bb();
|
2007-03-08 04:03:40 +08:00
|
|
|
type = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
/**
|
|
|
|
* @name POLY_BLOCK::compute_bb
|
2007-03-08 04:03:40 +08:00
|
|
|
*
|
|
|
|
* Compute the bounding box from the outline points.
|
2010-08-11 03:20:11 +08:00
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
void POLY_BLOCK::compute_bb() { //constructor
|
|
|
|
ICOORD ibl, itr; //integer bb
|
|
|
|
ICOORD botleft; //bounding box
|
|
|
|
ICOORD topright;
|
|
|
|
ICOORD pos; //current pos;
|
|
|
|
ICOORDELT_IT pts = &vertices; //iterator
|
|
|
|
|
|
|
|
botleft = *pts.data ();
|
|
|
|
topright = botleft;
|
|
|
|
do {
|
|
|
|
pos = *pts.data ();
|
|
|
|
if (pos.x () < botleft.x ())
|
|
|
|
//get bounding box
|
|
|
|
botleft = ICOORD (pos.x (), botleft.y ());
|
|
|
|
if (pos.y () < botleft.y ())
|
|
|
|
botleft = ICOORD (botleft.x (), pos.y ());
|
|
|
|
if (pos.x () > topright.x ())
|
|
|
|
topright = ICOORD (pos.x (), topright.y ());
|
|
|
|
if (pos.y () > topright.y ())
|
|
|
|
topright = ICOORD (topright.x (), pos.y ());
|
|
|
|
pts.forward ();
|
|
|
|
}
|
|
|
|
while (!pts.at_first ());
|
|
|
|
ibl = ICOORD (botleft.x (), botleft.y ());
|
|
|
|
itr = ICOORD (topright.x (), topright.y ());
|
2008-04-22 08:41:37 +08:00
|
|
|
box = TBOX (ibl, itr);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
/**
|
|
|
|
* @name POLY_BLOCK::winding_number
|
2007-03-08 04:03:40 +08:00
|
|
|
*
|
|
|
|
* Return the winding number of the outline around the given point.
|
2010-08-11 03:20:11 +08:00
|
|
|
* @param point point to wind around
|
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
inT16 POLY_BLOCK::winding_number(const ICOORD &point) {
|
2008-04-22 08:41:37 +08:00
|
|
|
inT16 count; //winding count
|
2007-03-08 04:03:40 +08:00
|
|
|
ICOORD pt; //current point
|
|
|
|
ICOORD vec; //point to current point
|
|
|
|
ICOORD vvec; //current point to next point
|
2008-04-22 08:41:37 +08:00
|
|
|
inT32 cross; //cross product
|
2007-03-08 04:03:40 +08:00
|
|
|
ICOORDELT_IT it = &vertices; //iterator
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
do {
|
|
|
|
pt = *it.data ();
|
|
|
|
vec = pt - point;
|
|
|
|
vvec = *it.data_relative (1) - pt;
|
|
|
|
//crossing the line
|
|
|
|
if (vec.y () <= 0 && vec.y () + vvec.y () > 0) {
|
|
|
|
cross = vec * vvec; //cross product
|
|
|
|
if (cross > 0)
|
|
|
|
count++; //crossing right half
|
|
|
|
else if (cross == 0)
|
|
|
|
return INTERSECTING; //going through point
|
|
|
|
}
|
|
|
|
else if (vec.y () > 0 && vec.y () + vvec.y () <= 0) {
|
|
|
|
cross = vec * vvec;
|
|
|
|
if (cross < 0)
|
|
|
|
count--; //crossing back
|
|
|
|
else if (cross == 0)
|
|
|
|
return INTERSECTING; //illegal
|
|
|
|
}
|
|
|
|
else if (vec.y () == 0 && vec.x () == 0)
|
|
|
|
return INTERSECTING;
|
|
|
|
it.forward ();
|
|
|
|
}
|
|
|
|
while (!it.at_first ());
|
|
|
|
return count; //winding number
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
/// @return true if other is inside this.
|
2009-07-11 10:14:57 +08:00
|
|
|
bool POLY_BLOCK::contains(POLY_BLOCK *other) {
|
|
|
|
inT16 count; // winding count
|
|
|
|
ICOORDELT_IT it = &vertices; // iterator
|
2007-03-08 04:03:40 +08:00
|
|
|
ICOORD vertex;
|
|
|
|
|
|
|
|
if (!box.overlap (*(other->bounding_box ())))
|
2009-07-11 10:14:57 +08:00
|
|
|
return false; // can't be contained
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
/* check that no vertex of this is inside other */
|
|
|
|
|
|
|
|
do {
|
|
|
|
vertex = *it.data ();
|
2009-07-11 10:14:57 +08:00
|
|
|
// get winding number
|
2007-03-08 04:03:40 +08:00
|
|
|
count = other->winding_number (vertex);
|
|
|
|
if (count != INTERSECTING)
|
|
|
|
if (count != 0)
|
2009-07-11 10:14:57 +08:00
|
|
|
return false;
|
2007-03-08 04:03:40 +08:00
|
|
|
it.forward ();
|
|
|
|
}
|
|
|
|
while (!it.at_first ());
|
|
|
|
|
|
|
|
/* check that all vertices of other are inside this */
|
|
|
|
|
|
|
|
//switch lists
|
|
|
|
it.set_to_list (other->points ());
|
|
|
|
do {
|
|
|
|
vertex = *it.data ();
|
|
|
|
//try other way round
|
|
|
|
count = winding_number (vertex);
|
|
|
|
if (count != INTERSECTING)
|
|
|
|
if (count == 0)
|
2009-07-11 10:14:57 +08:00
|
|
|
return false;
|
2007-03-08 04:03:40 +08:00
|
|
|
it.forward ();
|
|
|
|
}
|
|
|
|
while (!it.at_first ());
|
2009-07-11 10:14:57 +08:00
|
|
|
return true;
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
/**
|
|
|
|
* @name POLY_BLOCK::rotate
|
2007-03-08 04:03:40 +08:00
|
|
|
*
|
|
|
|
* Rotate the POLY_BLOCK.
|
2010-08-11 03:20:11 +08:00
|
|
|
* @param rotation cos, sin of angle
|
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
void POLY_BLOCK::rotate(FCOORD rotation) {
|
2007-03-08 04:03:40 +08:00
|
|
|
FCOORD pos; //current pos;
|
|
|
|
ICOORDELT *pt; //current point
|
|
|
|
ICOORDELT_IT pts = &vertices; //iterator
|
|
|
|
|
|
|
|
do {
|
|
|
|
pt = pts.data ();
|
|
|
|
pos.set_x (pt->x ());
|
|
|
|
pos.set_y (pt->y ());
|
|
|
|
pos.rotate (rotation);
|
2008-04-22 08:41:37 +08:00
|
|
|
pt->set_x ((inT16) (floor (pos.x () + 0.5)));
|
|
|
|
pt->set_y ((inT16) (floor (pos.y () + 0.5)));
|
2007-03-08 04:03:40 +08:00
|
|
|
pts.forward ();
|
|
|
|
}
|
|
|
|
while (!pts.at_first ());
|
2008-04-22 08:41:37 +08:00
|
|
|
compute_bb();
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
/**
|
2007-03-08 04:03:40 +08:00
|
|
|
* POLY_BLOCK::move
|
|
|
|
*
|
|
|
|
* Move the POLY_BLOCK.
|
2010-08-11 03:20:11 +08:00
|
|
|
* @param shift cos, sin of angle
|
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
void POLY_BLOCK::move(ICOORD shift) {
|
2007-03-08 04:03:40 +08:00
|
|
|
ICOORDELT *pt; //current point
|
|
|
|
ICOORDELT_IT pts = &vertices; //iterator
|
|
|
|
|
|
|
|
do {
|
|
|
|
pt = pts.data ();
|
|
|
|
*pt += shift;
|
|
|
|
pts.forward ();
|
|
|
|
}
|
|
|
|
while (!pts.at_first ());
|
2008-04-22 08:41:37 +08:00
|
|
|
compute_bb();
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef GRAPHICS_DISABLED
|
2009-07-11 10:14:57 +08:00
|
|
|
void POLY_BLOCK::plot(ScrollView* window, inT32 num) {
|
2007-03-08 04:03:40 +08:00
|
|
|
ICOORDELT_IT v = &vertices;
|
|
|
|
|
2009-07-11 10:14:57 +08:00
|
|
|
window->Pen(ColorForPolyBlockType(type));
|
2008-04-22 08:41:37 +08:00
|
|
|
|
2007-03-08 04:03:40 +08:00
|
|
|
v.move_to_first ();
|
|
|
|
|
|
|
|
if (num > 0) {
|
2008-02-01 08:36:18 +08:00
|
|
|
window->TextAttributes("Times", 80, false, false, false);
|
2007-03-08 04:03:40 +08:00
|
|
|
char temp_buff[34];
|
|
|
|
#ifdef __UNIX__
|
2008-04-22 08:41:37 +08:00
|
|
|
sprintf(temp_buff, INT32FORMAT, num);
|
2007-03-08 04:03:40 +08:00
|
|
|
#else
|
|
|
|
ltoa (num, temp_buff, 10);
|
|
|
|
#endif
|
2008-02-01 08:36:18 +08:00
|
|
|
window->Text(v.data ()->x (), v.data ()->y (), temp_buff);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2008-02-01 08:36:18 +08:00
|
|
|
|
|
|
|
window->SetCursor(v.data ()->x (), v.data ()->y ());
|
|
|
|
for (v.mark_cycle_pt (); !v.cycled_list (); v.forward ()) {
|
|
|
|
window->DrawTo(v.data ()->x (), v.data ()->y ());
|
|
|
|
}
|
2007-03-08 04:03:40 +08:00
|
|
|
v.move_to_first ();
|
2008-02-01 08:36:18 +08:00
|
|
|
window->DrawTo(v.data ()->x (), v.data ()->y ());
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-22 08:41:37 +08:00
|
|
|
void POLY_BLOCK::fill(ScrollView* window, ScrollView::Color colour) {
|
|
|
|
inT16 y;
|
|
|
|
inT16 width;
|
2007-03-08 04:03:40 +08:00
|
|
|
PB_LINE_IT *lines;
|
|
|
|
ICOORDELT_LIST *segments;
|
|
|
|
ICOORDELT_IT s_it;
|
|
|
|
|
|
|
|
lines = new PB_LINE_IT (this);
|
2008-04-22 08:41:37 +08:00
|
|
|
window->Pen(colour);
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
for (y = this->bounding_box ()->bottom ();
|
|
|
|
y <= this->bounding_box ()->top (); y++) {
|
|
|
|
segments = lines->get_line (y);
|
|
|
|
if (!segments->empty ()) {
|
|
|
|
s_it.set_to_list (segments);
|
|
|
|
for (s_it.mark_cycle_pt (); !s_it.cycled_list (); s_it.forward ()) {
|
|
|
|
// Note different use of ICOORDELT, x coord is x coord of pixel
|
|
|
|
// at the start of line segment, y coord is length of line segment
|
|
|
|
// Last pixel is start pixel + length.
|
|
|
|
width = s_it.data ()->y ();
|
2008-02-01 08:36:18 +08:00
|
|
|
window->SetCursor(s_it.data ()->x (), y);
|
|
|
|
window->DrawTo(s_it.data ()->x () + (float) width, y);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
/// @return true if the polygons of other and this overlap.
|
2009-07-11 10:14:57 +08:00
|
|
|
bool POLY_BLOCK::overlap(POLY_BLOCK *other) {
|
|
|
|
inT16 count; // winding count
|
|
|
|
ICOORDELT_IT it = &vertices; // iterator
|
2007-03-08 04:03:40 +08:00
|
|
|
ICOORD vertex;
|
|
|
|
|
2009-07-11 10:14:57 +08:00
|
|
|
if (!box.overlap(*(other->bounding_box())))
|
|
|
|
return false; // can't be any overlap.
|
2007-03-08 04:03:40 +08:00
|
|
|
|
|
|
|
/* see if a vertex of this is inside other */
|
|
|
|
|
|
|
|
do {
|
|
|
|
vertex = *it.data ();
|
2009-07-11 10:14:57 +08:00
|
|
|
// get winding number
|
2007-03-08 04:03:40 +08:00
|
|
|
count = other->winding_number (vertex);
|
|
|
|
if (count != INTERSECTING)
|
|
|
|
if (count != 0)
|
2009-07-11 10:14:57 +08:00
|
|
|
return true;
|
2007-03-08 04:03:40 +08:00
|
|
|
it.forward ();
|
|
|
|
}
|
|
|
|
while (!it.at_first ());
|
|
|
|
|
|
|
|
/* see if a vertex of other is inside this */
|
|
|
|
|
2009-07-11 10:14:57 +08:00
|
|
|
// switch lists
|
2007-03-08 04:03:40 +08:00
|
|
|
it.set_to_list (other->points ());
|
|
|
|
do {
|
2009-07-11 10:14:57 +08:00
|
|
|
vertex = *it.data();
|
|
|
|
// try other way round
|
2007-03-08 04:03:40 +08:00
|
|
|
count = winding_number (vertex);
|
|
|
|
if (count != INTERSECTING)
|
|
|
|
if (count != 0)
|
2009-07-11 10:14:57 +08:00
|
|
|
return true;
|
2007-03-08 04:03:40 +08:00
|
|
|
it.forward ();
|
|
|
|
}
|
|
|
|
while (!it.at_first ());
|
2009-07-11 10:14:57 +08:00
|
|
|
return false;
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-22 08:41:37 +08:00
|
|
|
ICOORDELT_LIST *PB_LINE_IT::get_line(inT16 y) {
|
2007-03-08 04:03:40 +08:00
|
|
|
ICOORDELT_IT v, r;
|
|
|
|
ICOORDELT_LIST *result;
|
|
|
|
ICOORDELT *x, *current, *previous;
|
|
|
|
float fy, fx;
|
|
|
|
|
|
|
|
fy = (float) (y + 0.5);
|
|
|
|
result = new ICOORDELT_LIST ();
|
|
|
|
r.set_to_list (result);
|
|
|
|
v.set_to_list (block->points ());
|
|
|
|
|
|
|
|
for (v.mark_cycle_pt (); !v.cycled_list (); v.forward ()) {
|
|
|
|
if (((v.data_relative (-1)->y () > y) && (v.data ()->y () <= y))
|
|
|
|
|| ((v.data_relative (-1)->y () <= y) && (v.data ()->y () > y))) {
|
|
|
|
previous = v.data_relative (-1);
|
|
|
|
current = v.data ();
|
|
|
|
fx = (float) (0.5 + previous->x () +
|
|
|
|
(current->x () - previous->x ()) * (fy -
|
|
|
|
previous->y ()) /
|
|
|
|
(current->y () - previous->y ()));
|
2008-04-22 08:41:37 +08:00
|
|
|
x = new ICOORDELT ((inT16) fx, 0);
|
2007-03-08 04:03:40 +08:00
|
|
|
r.add_to_end (x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!r.empty ()) {
|
|
|
|
r.sort (lessthan);
|
|
|
|
for (r.mark_cycle_pt (); !r.cycled_list (); r.forward ())
|
|
|
|
x = r.data ();
|
|
|
|
for (r.mark_cycle_pt (); !r.cycled_list (); r.forward ()) {
|
|
|
|
r.data ()->set_y (r.data_relative (1)->x () - r.data ()->x ());
|
|
|
|
r.forward ();
|
|
|
|
delete (r.extract ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-22 08:41:37 +08:00
|
|
|
int lessthan(const void *first, const void *second) {
|
2007-03-08 04:03:40 +08:00
|
|
|
ICOORDELT *p1 = (*(ICOORDELT **) first);
|
|
|
|
ICOORDELT *p2 = (*(ICOORDELT **) second);
|
|
|
|
|
|
|
|
if (p1->x () < p2->x ())
|
|
|
|
return (-1);
|
|
|
|
else if (p1->x () > p2->x ())
|
|
|
|
return (1);
|
|
|
|
else
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
/**
|
|
|
|
* @name POLY_BLOCK::serialise_asc
|
2007-03-08 04:03:40 +08:00
|
|
|
*
|
2010-08-11 03:20:11 +08:00
|
|
|
* Convert to ascii file.
|
|
|
|
* @param f file to use
|
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
void POLY_BLOCK::serialise_asc(FILE *f) {
|
2007-03-08 04:03:40 +08:00
|
|
|
vertices.serialise_asc (f);
|
|
|
|
box.serialise_asc (f);
|
2008-04-22 08:41:37 +08:00
|
|
|
serialise_INT32(f, type);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
/**
|
|
|
|
* @name POLY_BLOCK::de_serialise_asc
|
2007-03-08 04:03:40 +08:00
|
|
|
*
|
|
|
|
* Converto from ascii file.
|
2010-08-11 03:20:11 +08:00
|
|
|
* @param f file to use
|
|
|
|
*/
|
2007-03-08 04:03:40 +08:00
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
void POLY_BLOCK::de_serialise_asc(FILE *f) {
|
2007-03-08 04:03:40 +08:00
|
|
|
vertices.de_serialise_asc (f);
|
|
|
|
box.de_serialise_asc (f);
|
2009-07-11 10:14:57 +08:00
|
|
|
type = (PolyBlockType) de_serialise_INT32 (f);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2009-07-11 10:14:57 +08:00
|
|
|
|
|
|
|
|
2010-08-11 03:20:11 +08:00
|
|
|
/// Returns a color to draw the given type.
|
2009-07-11 10:14:57 +08:00
|
|
|
ScrollView::Color POLY_BLOCK::ColorForPolyBlockType(PolyBlockType type) {
|
|
|
|
const ScrollView::Color kPBColors[PT_COUNT] = {
|
|
|
|
ScrollView::WHITE,
|
|
|
|
ScrollView::BLUE,
|
|
|
|
ScrollView::CYAN,
|
|
|
|
ScrollView::MEDIUM_BLUE,
|
|
|
|
ScrollView::MAGENTA,
|
|
|
|
ScrollView::YELLOW,
|
|
|
|
ScrollView::RED,
|
|
|
|
ScrollView::MAROON,
|
|
|
|
ScrollView::ORANGE,
|
|
|
|
ScrollView::GREEN,
|
|
|
|
ScrollView::LIME_GREEN,
|
|
|
|
ScrollView::DARK_GREEN,
|
|
|
|
ScrollView::GREY
|
|
|
|
};
|
|
|
|
if (type >= 0 && type < PT_COUNT) {
|
|
|
|
return kPBColors[type];
|
|
|
|
}
|
|
|
|
return ScrollView::WHITE;
|
|
|
|
}
|
|
|
|
|