+++ /dev/null
-// image.cpp
-//
-// Part of the Architektonas Project
-// Originally part of QCad Community Edition by Andrew Mustun
-// Extensively rewritten and refactored by James L. Hammons
-// Portions copyright (C) 2001-2003 RibbonSoft
-// Copyright (C) 2010 Underground Software
-// See the README and GPLv2 files for licensing and warranty information
-//
-// JLH = James L. Hammons <jlhamm@acm.org>
-//
-// Who When What
-// --- ---------- -----------------------------------------------------------
-// JLH 05/28/2010 Added this text. :-)
-//
-
-#include "image.h"
-
-#include "constructionline.h"
-#include "debug.h"
-#include "graphicview.h"
-#include "line.h"
-#include "paintinterface.h"
-
-/**
- * Constructor.
- */
-Image::Image(EntityContainer * parent, const ImageData & d):
- AtomicEntity(parent), data(d)
-{
- update();
- calculateBorders();
-}
-
-/**
- * Destructor.
- */
-Image::~Image()
-{
- /*if (img!=NULL) {
- delete[] img;
- }*/
-}
-
-Entity * Image::clone()
-{
- Image * i = new Image(*this);
- i->setHandle(getHandle());
- i->initId();
- i->update();
- return i;
-}
-
-/** @return RS2::EntityImage */
-/*virtual*/ RS2::EntityType Image::rtti() const
-{
- return RS2::EntityImage;
-}
-
-void Image::update()
-{
- DEBUG->print("Image::update");
-
- // the whole image:
- //QImage image = QImage(data.file);
- img = QImage(data.file);
-
- if (!img.isNull())
- data.size = Vector(img.width(), img.height());
-
- DEBUG->print("Image::update: OK");
-
- /*
- // number of small images:
- nx = image.width()/100;
- ny = image.height()/100;
-
- // create small images:
- img = new QImage*[nx];
- RS_Pixmap pm;
- int w,h;
- for (int x = 0; x<nx; ++x) {
- img[x] = new QImage[ny];
- for (int y = 0; y<ny; ++y) {
- if (x<nx-1) {
- w = 100;
- }
- else {
- w = image.width()%100;
- }
-
- if (y<ny-1) {
- h = 100;
- }
- else {
- h = image.height()%100;
- }
-
- pm = RS_Pixmap(w, h);
- RS_PainterQt painter(&pm);
- painter.drawImage(-x*100, -y*100, image);
- img[x][y] = pm.convertToImage();
- }
- }
- */
-}
-
-/** @return Copy of data that defines the image. */
-ImageData Image::getData() const
-{
- return data;
-}
-
-/** @return Insertion point of the entity */
-/*virtual*/ Vector Image::getInsertionPoint() const
-{
- return data.insertionPoint;
-}
-
-/** Sets the insertion point for the image. */
-void Image::setInsertionPoint(Vector ip)
-{
- data.insertionPoint = ip;
- calculateBorders();
-}
-
-/** @return File name of the image. */
-QString Image::getFile() const
-{
- return data.file;
-}
-
-/** Sets the file name of the image. */
-void Image::setFile(const QString & file)
-{
- data.file = file;
-}
-
-/** @return u Vector. Points along bottom, 1 pixel long. */
-Vector Image::getUVector() const
-{
- return data.uVector;
-}
-
-/** @return v Vector. Points along left, 1 pixel long. */
-Vector Image::getVVector() const
-{
- return data.vVector;
-}
-
-/** @return Width of image in pixels. */
-int Image::getWidth() const
-{
- return (int)data.size.x;
-}
-
-/** @return Height of image in pixels. */
-int Image::getHeight() const
-{
- return (int)data.size.y;
-}
-
-/** @return Brightness. */
-int Image::getBrightness() const
-{
- return data.brightness;
-}
-
-/** @return Contrast. */
-int Image::getContrast() const
-{
- return data.contrast;
-}
-
-/** @return Fade. */
-int Image::getFade() const
-{
- return data.fade;
-}
-
-/** @return Image definition handle. */
-int Image::getHandle() const
-{
- return data.handle;
-}
-
-/** Sets the image definition handle. */
-void Image::setHandle(int h)
-{
- data.handle = h;
-}
-
-/** @return The four corners. **/
-VectorSolutions Image::getCorners()
-{
- VectorSolutions sol(4);
-
- sol.set(0, data.insertionPoint);
- sol.set(1,
- data.insertionPoint + data.uVector * Math::round(data.size.x));
- sol.set(3,
- data.insertionPoint + data.vVector * Math::round(data.size.y));
- sol.set(2, sol.get(3) + data.uVector * Math::round(data.size.x));
-
- return sol;
-}
-
-/**
- * @return image with in graphic units.
- */
-double Image::getImageWidth()
-{
- return data.size.x * data.uVector.magnitude();
-}
-
-/**
- * @return image height in graphic units.
- */
-double Image::getImageHeight()
-{
- return data.size.y * data.vVector.magnitude();
-}
-
-void Image::calculateBorders()
-{
- resetBorders();
- VectorSolutions sol = getCorners();
-
- for (int i = 0; i < 4; ++i)
- {
- minV = Vector::minimum(minV, sol.get(i));
- maxV = Vector::maximum(maxV, sol.get(i));
- }
-}
-
-Vector Image::getNearestEndpoint(const Vector & coord, double * dist)
-{
- VectorSolutions corners = getCorners();
- return corners.getClosest(coord, dist);
-}
-
-Vector Image::getNearestPointOnEntity(const Vector & coord, bool onEntity, double * dist, Entity * * entity)
-{
- if (entity != NULL)
- *entity = this;
-
- VectorSolutions corners = getCorners();
- VectorSolutions points(4);
-
- Line l[] = {
- Line(NULL, LineData(corners.get(0), corners.get(1))),
- Line(NULL, LineData(corners.get(1), corners.get(2))),
- Line(NULL, LineData(corners.get(2), corners.get(3))),
- Line(NULL, LineData(corners.get(3), corners.get(0)))
- };
-
- for (int i = 0; i < 4; ++i)
- points.set(i, l[i].getNearestPointOnEntity(coord, onEntity));
-
- return points.getClosest(coord, dist);
-}
-
-Vector Image::getNearestCenter(const Vector & coord, double * dist)
-{
- VectorSolutions points(4);
- VectorSolutions corners = getCorners();
-
- points.set(0, (corners.get(0) + corners.get(1)) / 2.0);
- points.set(1, (corners.get(1) + corners.get(2)) / 2.0);
- points.set(2, (corners.get(2) + corners.get(3)) / 2.0);
- points.set(3, (corners.get(3) + corners.get(0)) / 2.0);
-
- return points.getClosest(coord, dist);
-}
-
-Vector Image::getNearestMiddle(const Vector & coord, double * dist)
-{
- return getNearestCenter(coord, dist);
-}
-
-Vector Image::getNearestDist(double distance, const Vector & coord, double * dist)
-{
- VectorSolutions corners = getCorners();
- VectorSolutions points(4);
-
- Line l[] =
- {
- Line(NULL, LineData(corners.get(0), corners.get(1))),
- Line(NULL, LineData(corners.get(1), corners.get(2))),
- Line(NULL, LineData(corners.get(2), corners.get(3))),
- Line(NULL, LineData(corners.get(3), corners.get(0)))
- };
-
- for (int i = 0; i < 4; ++i)
- points.set(i, l[i].getNearestDist(distance, coord, dist));
-
- return points.getClosest(coord, dist);
-}
-
-double Image::getDistanceToPoint(const Vector & coord, Entity * * entity, RS2::ResolveLevel /*level*/, double /*solidDist*/)
-{
- if (entity != NULL)
- *entity = this;
-
- VectorSolutions corners = getCorners();
- double dist;
- double minDist = RS_MAXDOUBLE;
-
- Line l[] =
- {
- Line(NULL, LineData(corners.get(0), corners.get(1))),
- Line(NULL, LineData(corners.get(1), corners.get(2))),
- Line(NULL, LineData(corners.get(2), corners.get(3))),
- Line(NULL, LineData(corners.get(3), corners.get(0)))
- };
-
- for (int i = 0; i < 4; ++i)
- {
- dist = l[i].getDistanceToPoint(coord, NULL);
-
- if (dist < minDist)
- minDist = dist;
- }
-
- return minDist;
-}
-
-/*virtual*/ double Image::getLength()
-{
- return -1.0;
-}
-
-void Image::move(Vector offset)
-{
- data.insertionPoint.move(offset);
- calculateBorders();
-}
-
-void Image::rotate(Vector center, double angle)
-{
- data.insertionPoint.rotate(center, angle);
- data.uVector.rotate(angle);
- data.vVector.rotate(angle);
- calculateBorders();
-}
-
-void Image::scale(Vector center, Vector factor)
-{
- data.insertionPoint.scale(center, factor);
- data.uVector.scale(factor);
- data.vVector.scale(factor);
- calculateBorders();
-}
-
-void Image::mirror(Vector axisPoint1, Vector axisPoint2)
-{
- data.insertionPoint.mirror(axisPoint1, axisPoint2);
- data.uVector.mirror(Vector(0.0, 0.0), axisPoint2 - axisPoint1);
- data.vVector.mirror(Vector(0.0, 0.0), axisPoint2 - axisPoint1);
- calculateBorders();
-}
-
-void Image::draw(PaintInterface * painter, GraphicView * view, double /*patternOffset*/)
-{
- if (painter == NULL || view == NULL || img.isNull())
- return;
-
- // erase image:
- //if (painter->getPen().getColor()==view->getBackground()) {
- // VectorSolutions sol = getCorners();
- //
- //}
-
- int ox = 0;
- int oy = 0;
- int width = view->getWidth();
- int height = view->getHeight();
-
- Vector scale = Vector(view->toGuiDX(data.uVector.magnitude()),
- view->toGuiDY(data.vVector.magnitude()));
- double angle = data.uVector.angle();
-
- int startX, stopX, startY, stopY;
-
- if (data.uVector.x > 1.0e-6 && data.vVector.y > 1.0e-6)
- {
- startX = (int)((view->toGraphX(ox) - data.insertionPoint.x) / data.uVector.x) - 1;
-
- if (startX < 0)
- startX = 0;
-
- stopX = (int)((view->toGraphX(width) - data.insertionPoint.x) / data.uVector.x) + 1;
-
- if (stopX > (int)data.size.x)
- stopX = (int)data.size.x;
-
- startY = -(int)((view->toGraphY(oy) - (data.insertionPoint.y + getImageHeight()))
- / data.vVector.y) - 1;
-
- if (startY < 0)
- startY = 0;
-
- stopY = -(int)((view->toGraphY(height) - (data.insertionPoint.y + getImageHeight()))
- / data.vVector.y) + 1;
-
- if (stopY > (int)data.size.y)
- stopY = (int)data.size.y;
- }
- else
- {
- startX = 0;
- startY = 0;
- stopX = 0;
- stopY = 0;
- }
-
- painter->drawImg(img, view->toGui(data.insertionPoint), angle, scale,
- startX, startY, stopX - startX, stopY - startY);
-
- if (isSelected())
- {
- VectorSolutions sol = getCorners();
-
- painter->drawLine(view->toGui(sol.get(0)), view->toGui(sol.get(1)));
- painter->drawLine(view->toGui(sol.get(1)), view->toGui(sol.get(2)));
- painter->drawLine(view->toGui(sol.get(2)), view->toGui(sol.get(3)));
- painter->drawLine(view->toGui(sol.get(3)), view->toGui(sol.get(0)));
- }
-}
-
-/**
- * Dumps the point's data to stdout.
- */
-std::ostream & operator<<(std::ostream & os, const Image & i)
-{
- os << " Image: " << i.getData() << "\n";
- return os;
-}