summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/adaptagrams/libcola/box.cpp
blob: 8ad3cbaf64b1cdf6971837ef5f2ee43ba3d8f1e9 (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
/*
 * vim: ts=4 sw=4 et tw=0 wm=0
 *
 * libcola - A library providing force-directed network layout using the 
 *           stress-majorization method subject to separation constraints.
 *
 * Copyright (C) 2014  Monash University
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * See the file LICENSE.LGPL distributed with the library.
 *
 * This library 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.
 *
 * Author(s):  Michael Wybrow
 *
*/

#include "libcola/box.h"

namespace cola {

Box::Box(double xMin, double xMax, double yMin, double yMax)
{
    m_min[vpsc::XDIM] = nonNegative(xMin);
    m_max[vpsc::XDIM] = nonNegative(xMax);
    m_min[vpsc::YDIM] = nonNegative(yMin);
    m_max[vpsc::YDIM] = nonNegative(yMax);
}

Box::Box(double all)
{
    double value = nonNegative(all);
    m_min[vpsc::XDIM] = value;
    m_max[vpsc::XDIM] = value;
    m_min[vpsc::YDIM] = value;
    m_max[vpsc::YDIM] = value;
}

Box::Box()
{
    m_min[vpsc::XDIM] = 0;
    m_max[vpsc::XDIM] = 0;
    m_min[vpsc::YDIM] = 0;
    m_max[vpsc::YDIM] = 0;
}

Box::~Box()
{
}

bool Box::empty(void) const
{
    // values will be nonnegative so can sum to check if empty.
    int total = m_min[vpsc::XDIM] + m_max[vpsc::XDIM] + 
            m_min[vpsc::YDIM] + m_max[vpsc::YDIM];
    return (total == 0);
}

void Box::outputCode(FILE *fp) const
{
    if ((m_min[vpsc::XDIM] == m_max[vpsc::XDIM]) &&
        (m_min[vpsc::XDIM] == m_min[vpsc::YDIM]) &&
        (m_min[vpsc::XDIM] == m_max[vpsc::YDIM]))
    {
        fprintf(fp, "Box(%g)", m_min[vpsc::XDIM]);
    }
    else
    {
        fprintf(fp, "Box(%g, %g, %g, %g)", m_min[vpsc::XDIM],
                m_max[vpsc::XDIM], m_min[vpsc::YDIM], m_max[vpsc::YDIM]);
    }
}

vpsc::Rectangle Box::rectangleByApplyingBox(
        const vpsc::Rectangle rectangle) const
{
    if (!rectangle.isValid())
    {
        return rectangle;
    }

    return vpsc::Rectangle(
            rectangle.getMinX() - m_min[vpsc::XDIM], 
            rectangle.getMaxX() + m_max[vpsc::XDIM], 
            rectangle.getMinY() - m_min[vpsc::YDIM], 
            rectangle.getMaxY() + m_max[vpsc::YDIM]);
}

double Box::nonNegative(double value)
{
    return (value >= 0) ? value: 0;
}

double Box::min(size_t dim) const
{
    return (dim < 2) ? m_min[dim] : 0;
}

double Box::max(size_t dim) const
{
    return (dim < 2) ? m_max[dim] : 0;
}


}