-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_buoyancy.cpp
More file actions
188 lines (131 loc) · 4.91 KB
/
Copy pathtest_buoyancy.cpp
File metadata and controls
188 lines (131 loc) · 4.91 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include "Draw2D.h"
#include "fastmath.h"
#include "Vec2.h"
#include "VecN.h"
#include "PolyLinear1d.h"
#include "geom2D.h"
#include "Convex2d.h"
#include "AppSDL2OGL.h"
#include "buoyancy.h"
#include "testUtils.h"
// ====================== TestApp
class TestAppTerrainCubic : public AppSDL2OGL {
public:
double angle =0.0d;
double dAngle=0.0005d;
constexpr static int nPhis = 500;
double phis[ nPhis ];
double wts [ nPhis ];
double Ms [ nPhis ];
constexpr static int np = 4;
Convex2d * hull;
double yLs [ np ];
double yRs [ np ];
double xs [ np ];
// ---- function declarations
virtual void draw ();
virtual void drawHUD();
void evalPolar( double phi_min, double phi_max, double displacement );
TestAppTerrainCubic( int& id, int WIDTH_, int HEIGHT_ );
};
TestAppTerrainCubic::TestAppTerrainCubic( int& id, int WIDTH_, int HEIGHT_ ) : AppSDL2OGL( id, WIDTH_, HEIGHT_ ) {
hull = new Convex2d( np );
hull->corners[0].set( -1.0, -1.0 );
hull->corners[1].set( +1.0, -1.0 );
hull->corners[2].set( +1.0, +1.0 );
hull->corners[3].set( -1.0, +1.0 );
/*
hull->corners[0].set( -1.0, -1.0 );
hull->corners[1].set( +1.0, -1.0 );
hull->corners[2].set( +1.5, +1.0 );
hull->corners[3].set( -1.5, +1.0 );
*/
/*
hull->corners[0].set( -1.5, -1.0 );
hull->corners[1].set( +1.5, -1.0 );
hull->corners[2].set( +1.0, +1.0 );
hull->corners[3].set( -1.0, +1.0 );
*/
evalPolar( -1.5, 1.5, 2.0 );
//exit(0);
}
void TestAppTerrainCubic::evalPolar( double phi_min, double phi_max, double displacement ){
double dphi = ( phi_max - phi_min ) / (nPhis-1);
for( int i=0; i<nPhis; i++ ){
double phi = phi_min + dphi * i;
Vec2d dir; dir.set( sin( phi ), cos( phi ) );
hull->projectToLine( dir, xs, yLs, yRs );
double watterline;
double moment = integrate_moment( np, xs, yLs, yRs, displacement, watterline );
phis[i] = phi;
Ms [i] = moment;
wts [i] = watterline;
printf( " %i %f %f %f \n", i, phi, moment, watterline );
}
}
void TestAppTerrainCubic::draw(){
glClearColor( 1.0f, 1.0f, 1.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glDisable( GL_DEPTH_TEST );
// ============== project to line Convex Polygons
glColor3f( 0.7f, 0.7f, 0.7f ); Draw2D::drawConvexPolygon( np, hull->corners, false );
//Vec2d dir; dir.set( +1.0, 0.1 ); dir.normalize( );
//ouble angle = ( frameCount%100 - 50 ) * 0.007;
//angle = 0.3d;
angle += dAngle;
if ( ( angle > 1.5 ) && ( dAngle > 0 ) ){ dAngle = -dAngle; }
else if ( ( angle < -1.5 ) && ( dAngle < 0 ) ){ dAngle = -dAngle; };
Vec2d dir; dir.set( sin( angle ), cos( angle ) );
hull->projectToLine( dir, xs, yLs, yRs );
//printArray( base.n, xs );
//printArray( base.n, yLs );
//printArray( base.n, yRs );
double ys [ np ];
VecN::sub( np, yLs, yRs, ys );
double displacement = 2.0;
/*
PolyLinear1d pline( np, xs, ys );
pline.n = np;
pline.xs = xs;
pline.ys = ys;
//double V = pline.integrate( -1000.0, 1000.0 );
double V = pline.integrate_ibounds( 0, pline.n );
double x_disp = pline.x_of_integral( displacement );
//double V_disp = pline.integrate( -1000.0, x_disp );
double V_disp = NAN;
printf( " V %f xhalf %f Vhalf %f Vtarget %f \n", V, x_disp, V_disp, displacement );
pline.detach();
*/
double watterline;
double moment = integrate_moment( np, xs, yLs, yRs, displacement, watterline );
//printf( " V %f xtarget %f %f Vtarget %f %f moment %f \n", V, x_disp, watterline, V_disp, displacement, moment );
//printf( " %i %f %f \n", frameCount, watterline, moment );
glColor3f( 0.2f, 0.9f, 0.2f ); Draw2D::drawLine( { -10.0, watterline }, { +10.0, watterline } );
glColor3f( 0.9f, 0.9f, 0.9f ); Draw2D::drawLine( { 0.0, -10.0 }, { 0.0, +10.0 } ); Draw2D::drawLine( {-10.0, 0.0 }, {+10.0, 0.0 } );
glColor3f( 0.9f, 0.2f, 0.2f ); Draw2D::plot( np, yLs, xs );
glColor3f( 0.2f, 0.2f, 0.9f ); Draw2D::plot( np, yRs, xs );
glColor3f( 0.0f, 0.0f, 0.0f ); Draw2D::plot( np, ys, xs );
glColor3f( 0.9f, 0.2f, 0.9f ); Draw2D::plot( nPhis, phis, Ms ); Draw2D::drawPointCross_d( { angle, moment }, 0.1 );
glColor3f( 0.2f, 0.7f, 0.9f ); Draw2D::plot( nPhis, phis, wts ); Draw2D::drawPointCross_d( { angle, watterline }, 0.1 );
// ============== Mouse Raycast Convex Polygons
//STOP = true;
};
void TestAppTerrainCubic::drawHUD(){}
// ===================== MAIN
TestAppTerrainCubic * testApp;
int main(int argc, char *argv[]){
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
int junk;
testApp = new TestAppTerrainCubic( junk , 800, 600 );
testApp->loop( 1000000 );
return 0;
}