/*
* EtherPix simulator
*
* Copyright 2017 Stefan Schuermans <stefan schuermans info>
*
* This program 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, version 3 of the License.
*
*
* This program 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 Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bbox.h"
#include "transform.h"
/**
* @brief constructor
* @param[in] bb boundig box of input coordinates
* @param[in] width width of output coordinate speace (left to right)
* @param[in] height height of output coordinate space (top to bottom)
*/
Transform::Transform(BBox const &bb, double width, double height):
m_init(false),
m_zoom(1.0),
m_shift_x(0.0),
m_shift_y(0.0)
{
double bb_x_min, bb_x_max, bb_y_min, bb_y_max;
if (! bb.get(bb_x_min, bb_x_max, bb_y_min, bb_y_max))
return;
double delta_x = bb_x_max - bb_x_min;
double delta_y = bb_y_max - bb_y_min;
if (delta_x <= 0.0 || delta_y <= 0.0)
return;
// compute zoom factor (keep aspect ration, fit into window)
double zoom_x = (double)width / delta_x;
double zoom_y = (double)height / delta_y;
m_zoom = zoom_x < zoom_y ? zoom_x : zoom_y;
// compute shift (center of pixels should be center of window)
m_shift_x = 0.5 * (double)width - 0.5 * (bb_x_min + bb_x_max) * m_zoom;
m_shift_y = 0.5 * (double)height - 0.5 * (bb_y_min + bb_y_max) * -m_zoom;