File: boatmodel.cpp

package info (click to toggle)
boats 201204-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,352 kB
  • sloc: cpp: 8,770; makefile: 48; xml: 10
file content (230 lines) | stat: -rw-r--r-- 6,534 bytes parent folder | download
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// C++ Implementation: boatmodel
//
// Description:
//
//
// Author: Thibaut GRIDEL <tgridel@free.fr>
//
// Copyright (c) 2008-2011 Thibaut GRIDEL
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
//
//
#include <iostream>
#include <cmath>

#include "boatmodel.h"

#include "commontypes.h"
#include "situationmodel.h"
#include "trackmodel.h"

extern int debugLevel;

BoatModel::BoatModel(TrackModel* track, QObject *parent)
        : PositionModel(track->situation(), parent),
        m_heading(0),
        m_trim(0),
        m_spin(false),
        m_spinTrim(0),
        m_overlap(Boats::none),
        m_flag(Boats::noFlag),
        m_hidden(false),
        m_acceleration(Boats::constant),
        m_track(track),
        m_dim(255) {
    if (debugLevel & 1 << MODEL) std::cout << "new Boat " << this << std::endl;
    setOrder(track->size()+1);
}

BoatModel::~BoatModel() {
    if (debugLevel & 1 << MODEL) std::cout << "delete Boat " << this << std::endl;
}

void BoatModel::setHeading(const qreal& theValue) {
    if (theValue != m_heading) {
        if (debugLevel & 1 << MODEL) std::cout
                << "heading = " << theValue  << std::endl;
        m_heading = fmod(theValue+360.0,360.0);
        emit headingChanged(m_heading);
        setTrimmedSailAngle(trimmedSailAngle());
        if (m_spin) {
            emit trimmedSpinAngleChanged(spinAngle() + m_spinTrim);
        }
        m_track->changingTrack(m_track);
    }
}

void BoatModel::setPosition(const QPointF& theValue) {
    PositionModel::setPosition(theValue);
    m_track->changingTrack(m_track);
}

void BoatModel::setTrim(const qreal& theValue) {
    qreal newAngle = sailAngle() + theValue;
    if (theValue != m_trim
        && newAngle < 180
        && newAngle > -180) {
        m_trim = theValue;
        if (debugLevel & 1 << MODEL) std::cout
                << "trim = " << theValue  << std::endl;
        emit trimChanged(m_trim);
        setTrimmedSailAngle(trimmedSailAngle());
    }
}

void BoatModel::setTrimmedSailAngle(qreal theValue) {
    emit trimmedSailAngleChanged(theValue);
}

void BoatModel::setSpin(const bool theValue) {
    if (theValue != m_spin) {
        m_spin = theValue;
        emit spinChanged(m_spin);
    }
}

void BoatModel::setSpinTrim(const qreal& theValue) {
    qreal sailAngle = spinAngle();
    qreal newAngle = sailAngle + theValue;
    if (theValue != m_spinTrim
        && newAngle < 180
        && newAngle > -180) {
        m_spinTrim = theValue;
        if (debugLevel & 1 << MODEL) std::cout
                << "spinTrim = " << theValue  << std::endl;
        emit spinTrimChanged(m_spinTrim);
        emit trimmedSpinAngleChanged(spinAngle() + m_spinTrim);
    }
}

void BoatModel::setOverlap(const  Boats::Overlaps theValue) {
    if (theValue != m_overlap) {
        if (debugLevel & 1 << MODEL) std::cout
                << "overlap = " << theValue  << std::endl;
        m_overlap = theValue;
        emit overlapChanged(m_overlap);
    }
}

void BoatModel::setFlag(const  Boats::Flag theValue) {
    if (theValue != m_flag) {
        if (debugLevel & 1 << MODEL) std::cout
                << "flag = " << theValue  << std::endl;
        m_flag = theValue;
        emit flagChanged(m_flag);
    }
}

void BoatModel::setHidden(const  bool theValue) {
    if (theValue != m_hidden) {
        if (debugLevel & 1 << MODEL) std::cout
                << "hidden = " << theValue  << std::endl;
        m_hidden = theValue;
        emit hiddenChanged(m_hidden);
    }
}

void BoatModel::setAcceleration(const  Boats::Acceleration theValue) {
    if (theValue != m_acceleration) {
        if (debugLevel & 1 << MODEL) std::cout
                << "acceleration = " << theValue  << std::endl;
        m_acceleration = theValue;
    }
}

qreal BoatModel::sailAngle(qreal heading) const {
    qreal layline = situation()->laylineAngle();
    qreal sailAngle;

    if (heading == -1) {
        heading = fmod(m_heading - wind() + 360, 360);
    }
    // within 10° inside layline angle, the sail is headed
    if (heading < layline-10) {
        sailAngle =  heading;
    } else if (heading > 360 - (layline-10)) {
        sailAngle =  heading - 360;
    } else {

        switch (m_track->series()) {
            // tornado has fixed 20° incidence
        case Boats::tornado:
            if (heading<180) {
                sailAngle = 20;
            } else {
                sailAngle = - 20;
            }
            break;
        default:
            // linear incidence variation
            // incidence is 15° at layline angle and 90° downwind
            qreal a = (180 - layline) / 75;
            qreal b = layline / a - 15;
            if (heading<180) {
                sailAngle = heading/a - b;
            } else {
                sailAngle = heading/a - b - 180;
            }
            break;
        }
    }

    if (debugLevel & 1 << MODEL) std::cout
            << "heading = " << heading
            << " sail = " << sailAngle
            << std::endl;
    return sailAngle;
}

qreal BoatModel::spinAngle(qreal heading) const {
    qreal sailAngle;

    if (heading == -1) {
        heading = m_heading;
    }
    // within 10° above downwind angle, the sail is headed
    if (heading < 80) {
        sailAngle =  heading;
    } else if (heading > 280) {
        sailAngle =  heading - 360;
    } else {
        if (heading<180) {
            sailAngle = heading - 20;
        } else {
            sailAngle = heading + 20;
        }
    }

    if (debugLevel & 1 << MODEL) std::cout
            << "heading = " << heading
            << " spin = " << sailAngle
            << std::endl;
    return sailAngle;
}

void BoatModel::setWind(qreal wind) {
    PositionModel::setWind(wind);
    setTrimmedSailAngle(trimmedSailAngle());
}

void BoatModel::setDim(int dim) {
    m_dim = dim;
    emit dimChanged(dim);
}

void BoatModel::setPath(QPainterPath path) {
    m_path = path;
}