File: shape.h

package info (click to toggle)
contextfree 3.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,260 kB
  • sloc: cpp: 37,992; lex: 414; makefile: 123; sh: 43; python: 34
file content (211 lines) | stat: -rw-r--r-- 6,571 bytes parent folder | download | duplicates (2)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// shape.h
// this file is part of Context Free
// ---------------------
// Copyright (C) 2005-2008 Mark Lentczner - markl@glyphic.com
// Copyright (C) 2005-2015 John Horigan - john@glyphic.com
//
// 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; either version 2
// of the License, or (at your option) any later version.
//
// 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 General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// 
// John Horigan can be contacted at john@glyphic.com or at
// John Horigan, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
// Mark Lentczner can be contacted at markl@glyphic.com or at
// Mark Lentczner, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
//


#ifndef INCLUDE_SHAPE_H
#define INCLUDE_SHAPE_H

#include <iostream>
#include <cmath>
#include <functional>

#include "agg2/agg_math_stroke.h"
#include "agg2/agg_trans_affine.h"
#include "agg_trans_affine_1D.h"
#include "agg_trans_affine_time.h"
#include "agg2/agg_color_rgba.h"
#include "agg2/agg_pixfmt_rgba.h"
#include "HSBColor.h"
#include "Rand64.h"
#include "bounds.h"
#include "stacktype.h"

// Contains all of the information about a change to a shape 
class Modification {
    public: 
        agg::trans_affine m_transform;
        agg::trans_affine_1D m_Z;
        agg::trans_affine_time m_time;
        
        HSBColor m_Color;
        HSBColor m_ColorTarget;
        unsigned m_ColorAssignment = 0;
        int      m_BlendMode = 0;

        Rand64 mRand64Seed;

        Modification() = default;
    
        double area() const { return fabs(m_transform.determinant()); }
        bool isFinite() const;
        
        Modification operator*(const Modification& m) const
        {
            Modification n = *this;
            n *= m;
            return n;
        }
        Modification& operator*=(const Modification& m)
        {
            m_transform.premultiply(m.m_transform);
            m_Z.premultiply(m.m_Z);
            m_time.premultiply(m.m_time);
            HSBColor::Adjust(m_Color, m_ColorTarget, m.m_Color, m.m_ColorTarget,
                             m.m_ColorAssignment);
            mRand64Seed ^= m.mRand64Seed;
            if (m.m_BlendMode)
                m_BlendMode = m.m_BlendMode;
            return *this;
        }
    
    bool merge(const Modification& m);
};

class ShapeBase {
public: 
    int mShapeType = -1;
    Modification mWorldState;
    
    double mAreaCache;
    double area() const { return mAreaCache; }
protected:
    ShapeBase() 
    { mAreaCache = mWorldState.area(); }
    
    void write(std::ostream& os) const;
    void read(std::istream& is);
};

// Contains all of the information about a shape that is used during parsing
// and executing a cfdg file
class Shape : public ShapeBase {
public:
    Shape() = default;
    Shape(const Shape&) = default;
    Shape(Shape&& s) noexcept
    : ShapeBase(s), mParameters(std::move(s.mParameters))
    { }
    ~Shape() = default;
    Shape& operator=(const Shape& o) {
        if (this == &o) return *this;
        mShapeType = o.mShapeType;
        mWorldState = o.mWorldState;
        mAreaCache = o.mAreaCache;
        mParameters = o.mParameters;
        return *this;
    }
    Shape& operator=(Shape&& o) noexcept {
        if (this == &o) return *this;
        mShapeType = o.mShapeType;
        mWorldState = o.mWorldState;
        mAreaCache = o.mAreaCache;
        mParameters = std::move(o.mParameters);
        return *this;
    }
    
    param_ptr mParameters;

    Shape operator*(const Modification& m) const {
        Shape s = *this;
        s.mWorldState *= m;
        s.mAreaCache = s.mWorldState.area();
        return s;
    }
    
    Shape& operator*=(const Modification& m) {
        mWorldState *= m;
        mAreaCache = mWorldState.area();
        return *this;
    }
    
    bool operator<(const Shape& b) const { return mAreaCache < b.mAreaCache; }
    
    void write(std::ostream& os) const;
    void read(std::istream& is);
protected:
    void writeParams(std::ostream& os) const;
    void readParams(std::istream& is);
};

class FinishedShape : public Shape {
public:
    Bounds mBounds;
    FinishedShape() = default;
    FinishedShape(Shape&& s, int order, const Bounds& b) noexcept
    {
        mShapeType = s.mShapeType;
        mWorldState = s.mWorldState;
        mWorldState.m_ColorAssignment = static_cast<unsigned>(order);
        mParameters = s.mParameters;
        mBounds = b;
    }
    FinishedShape(const FinishedShape&) = default;
    FinishedShape(FinishedShape&&) noexcept = default;
    FinishedShape& operator=(const FinishedShape& o) {
        if (this == &o) return *this;
        mShapeType = o.mShapeType;
        mWorldState = o.mWorldState;
        mAreaCache = o.mAreaCache;
        mParameters = o.mParameters;
        mBounds = o.mBounds;
        return *this;
    }
    FinishedShape& operator=(FinishedShape&& o) noexcept {
        if (this == &o) return *this;
        mShapeType = o.mShapeType;
        mWorldState = o.mWorldState;
        mAreaCache = o.mAreaCache;
        mParameters = std::move(o.mParameters);
        mBounds = o.mBounds;
        return *this;
    }

    bool operator<(const Shape& b) const
    {
        return (mWorldState.m_Z.tz == b.mWorldState.m_Z.tz) ?
            (mWorldState.m_ColorAssignment < b.mWorldState.m_ColorAssignment) :
            (mWorldState.m_Z.tz < b.mWorldState.m_Z.tz);
    }
    
    void write(std::ostream& os) const;
    void read(std::istream& is);
};

inline std::ostream& operator<<(std::ostream& os, const Shape& s) { s.write(os); return os; }
inline std::istream& operator>>(std::istream& is, Shape& s) { s.read(is); return is; }
inline std::ostream& operator<<(std::ostream& os, const FinishedShape& s) { s.write(os); return os; }
inline std::istream& operator>>(std::istream& is, FinishedShape& s) { s.read(is); return is; }


const double MY_PI =  3.14159265358979323846;
const int ModificationSize = (sizeof(Modification) + 7) >> 3;

using ShapeFunction = std::function<void (const FinishedShape&)>;
        

#endif // INCLUDE_SHAPE_H