summaryrefslogtreecommitdiffstats
path: root/game/code/roads/roadsegmentdata.cpp
blob: ff27bcae468170417a2f7ad2ec33e6c8d77be41c (plain) (blame)
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
/*===========================================================================
 Copyright (C) 2000 Radical Entertainment Ltd.  All rights reserved.

 Component:   RoadSegment

 Description:


 Authors:     Travis Brown-John

 Revisions    Date            Author      Revision
              2002/02/25      Tbrown-John Created

===========================================================================*/
#include <memory/classsizetracker.h>
#include <roads/roadsegmentdata.h>

// System.
//
#include <raddebug.hpp>

/*

  Input parameters:

 O----------> v0
 |          |
 |          |
 |          |
 |          |
 v          v
 v2         v1

  O = ( 0.0f, 0.0f, 0.0f ) LCS origin.

  # of lanes.

  create this:

                       |
                N1    \ / 
       v0_________________________v1----> facing
        |                         |
  -> N0 |           --> direction | -> N2
        |_________________________|
       v3						  v2
                N3    / \ 
                       |
  N is UP
  N0 is  ->

  N1 is  |
        \ /

  N2 is  ->

  N3 is / \
         |
 */
RoadSegmentData::RoadSegmentData() :
    mnLanes( 0 )
{
    CLASSTRACKER_CREATE( RoadSegmentData );
    unsigned int i;
    for ( i = 0; i < 4; ++i )
    {
        mCorners[i].Set(0.0f, 0.0f, 0.0f);
        mEdgeNormals[i].Set(0.0f, 0.0f, 0.0f);
    }

    mNormal.Set(0.0f, 0.0f, 0.0f);
    
}

RoadSegmentData::RoadSegmentData(
        const rmt::Vector& v0, 
        const rmt::Vector& v1, 
        const rmt::Vector& v2,
        unsigned int nLanes )
{
    CLASSTRACKER_CREATE( RoadSegmentData );
    SetData( v0, v1, v2, nLanes );
}

RoadSegmentData::~RoadSegmentData()
{
    CLASSTRACKER_DESTROY( RoadSegmentData );
}

//=============================================================================
// RoadSegmentData::SetData
//=============================================================================
// Description: Comment
//
// Parameters:  ( const rmt::Vector& v0, const rmt::Vector& v1, const rmt::Vector& v2, unsigned int nLanes, bool hasShoulder )
//
// Return:      void 
//
//=============================================================================
void RoadSegmentData::SetData( const rmt::Vector& v0, 
                               const rmt::Vector& v1, 
                               const rmt::Vector& v2, 
                               unsigned int nLanes )
{
    mnLanes = nLanes;

    // Calculate the up vector for this piece.
    //
    mNormal.CrossProduct( v0, v2 );
    mNormal.Normalize( );

    rmt::Vector origin( 0.0f, 0.0f, 0.0f );

    mCorners[ 0 ] = origin;
    mCorners[ 1 ] = v0;
    mCorners[ 2 ] = v1;
    mCorners[ 3 ] = v2;

    rmt::Vector temp;

    // Calculate the edge normals.
    temp.Sub( mCorners[ 0 ], mCorners[ 3 ] );
    mEdgeNormals[ 0 ].CrossProduct( mNormal, temp );
    mEdgeNormals[ 0 ].Normalize( );
    
    temp.Sub( mCorners[ 1 ], mCorners[ 0 ] );
    mEdgeNormals[ 1 ].CrossProduct( mNormal, temp );
    mEdgeNormals[ 1 ].Normalize( );

    temp.Sub( mCorners[ 1 ], mCorners[ 2 ] );
    mEdgeNormals[ 2 ].CrossProduct( mNormal, temp );
    mEdgeNormals[ 2 ].Normalize( );

    temp.Sub( mCorners[ 3 ], mCorners[ 2 ] );
    mEdgeNormals[ 3 ].CrossProduct( mNormal, temp );
    mEdgeNormals[ 3 ].Normalize( );


}

/*
==============================================================================
RoadSegmentData::GetCorner
==============================================================================
Description:    Comment


Parameters:     ( int index ), which box corner are we interested in.

Return:         const rmt::Vector& - a copy of the vertex location in local space.

=============================================================================
*/
const rmt::Vector& RoadSegmentData::GetCorner( int index ) const
{
    rAssert( index < 4 );
    return mCorners[ index ];
}
/*
==============================================================================
RoadSegmentData::GetEdgeNormal
==============================================================================
Description:    Return the normal to the edge.  Define CW, starting 
                with leading edge.  v4.Sub( v0 ) = leading edge.

Parameters:     ( int index )

Return:         const 

=============================================================================
*/
const rmt::Vector& RoadSegmentData::GetEdgeNormal( int index ) const
{
    rAssert( index < 4 );
    return mEdgeNormals[ index ];
}

/*
==============================================================================
RoadSegmentData::GetSegmentNormal
==============================================================================
Description:    Return the normal to the planar segment surface.

Parameters:     ( void )

Return:         const rmt::Vector&

=============================================================================
*/
const rmt::Vector& RoadSegmentData::GetSegmentNormal( void ) const
{
    return mNormal;
}

/*
==============================================================================
RoadSegmentData::GetNumLanes
==============================================================================
Description:    Return the number of lanes, on the left side if bIsLeft is true.
                Else, the number of lanes on the right side.

Parameters:     ( void )

Return:         unsigned int, the number of lanes.

=============================================================================
*/
unsigned int RoadSegmentData::GetNumLanes( void ) const
{
    return mnLanes;
}