netradiant-custom/libs/dragplanes.h

228 lines
8.0 KiB
C++

/*
Copyright (C) 2001-2006, William Joseph.
All Rights Reserved.
This file is part of GtkRadiant.
GtkRadiant 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.
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if !defined( INCLUDED_DRAGPLANES_H )
#define INCLUDED_DRAGPLANES_H
#include "selectable.h"
#include "selectionlib.h"
#include "math/aabb.h"
#include "math/line.h"
// local must be a pure rotation
inline Vector3 translation_to_local( const Vector3& translation, const Matrix4& local ){
return matrix4_get_translation_vec3(
matrix4_multiplied_by_matrix4(
matrix4_translated_by_vec3( matrix4_transposed( local ), translation ),
local
)
);
}
// local must be a pure rotation
inline Vector3 translation_from_local( const Vector3& translation, const Matrix4& local ){
return matrix4_get_translation_vec3(
matrix4_multiplied_by_matrix4(
matrix4_translated_by_vec3( local, translation ),
matrix4_transposed( local )
)
);
}
#if 0
namespace{
// https://www.gamedev.net/forums/topic/230443-initializing-array-member-objects-in-c/?page=2
class ObservedSelectables6x {
ObservedSelectable m_selectable0, m_selectable1, m_selectable2, m_selectable3, m_selectable4, m_selectable5;
// array of 6 pointers to ObservedSelectable members of ObservedSelectables6x
static ObservedSelectable ObservedSelectables6x::* const array[6];
public:
ObservedSelectables6x( const SelectionChangeCallback& onchanged ) :
m_selectable0( onchanged ),
m_selectable1( onchanged ),
m_selectable2( onchanged ),
m_selectable3( onchanged ),
m_selectable4( onchanged ),
m_selectable5( onchanged ) {
}
ObservedSelectable& operator[]( std::size_t idx ) {
return this->*array[idx];
}
const ObservedSelectable& operator[]( std::size_t idx ) const {
return this->*array[idx];
}
};
// Parse this
ObservedSelectable ObservedSelectables6x::* const ObservedSelectables6x::array[6] = { &ObservedSelectables6x::m_selectable0,
&ObservedSelectables6x::m_selectable1,
&ObservedSelectables6x::m_selectable2,
&ObservedSelectables6x::m_selectable3,
&ObservedSelectables6x::m_selectable4,
&ObservedSelectables6x::m_selectable5 };
} //namespace
#endif
class DragPlanes
{
std::vector<ObservedSelectable> m_selectables;
public:
AABB m_bounds;
DragPlanes( const SelectionChangeCallback& onchanged ){
for ( std::size_t i = 0; i < 6; ++i )
m_selectables.push_back( ObservedSelectable( onchanged ) );
}
bool isSelected() const {
for ( std::size_t i = 0; i < 6; ++i )
if( m_selectables[i].isSelected() )
return true;
return false;
}
void setSelected( bool selected ){
for ( std::size_t i = 0; i < 6; ++i )
m_selectables[i].setSelected( selected );
}
void selectPlanes( const AABB& aabb, Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback, const Matrix4& rotation = g_matrix4_identity ){
Vector3 corners[8];
aabb_corners_oriented( aabb, rotation, corners );
Plane3 planes[6];
aabb_planes_oriented( aabb, rotation, planes );
const std::size_t indices[24] = {
2, 1, 5, 6, //+x //right
3, 7, 4, 0, //-x //left
1, 0, 4, 5, //+y //front
3, 2, 6, 7, //-y //back
0, 1, 2, 3, //+z //top
7, 6, 5, 4, //-z //bottom
};
const Vector3 viewdir( vector3_normalised( Vector3( test.getVolume().GetModelview()[2], test.getVolume().GetModelview()[6], test.getVolume().GetModelview()[10] ) ) );
double bestDot = 1;
ObservedSelectable* selectable = 0;
ObservedSelectable* selectable2 = 0;
for ( std::size_t i = 0; i < 6; ++i ){
const std::size_t index = i * 4;
const Vector3 centroid = vector3_mid( corners[indices[index]], corners[indices[index + 2]] );
const Vector3 projected = vector4_projected( matrix4_transformed_vector4( test.getVolume().GetViewMatrix(), Vector4( centroid, 1 ) ) );
const Vector3 closest_point = vector4_projected( matrix4_transformed_vector4( test.getScreen2world(), Vector4( 0, 0, projected[2], 1 ) ) );
if ( vector3_dot( planes[i].normal(), closest_point - corners[indices[index]] ) > 0
&& vector3_dot( planes[i].normal(), closest_point - corners[indices[index + 1]] ) > 0
&& vector3_dot( planes[i].normal(), closest_point - corners[indices[index + 2]] ) > 0
&& vector3_dot( planes[i].normal(), closest_point - corners[indices[index + 3]] ) > 0 ) {
const double dot = fabs( vector3_dot( planes[i].normal(), viewdir ) );
const double diff = bestDot - dot;
if( diff > 0.03 ){
bestDot = dot;
selectable = &m_selectables[i];
selectable2 = 0;
}
else if( fabs( diff ) <= 0.03 ){
selectable2 = &m_selectables[i];
}
}
}
if( test.getVolume().GetViewMatrix().xw() != 0 || test.getVolume().GetViewMatrix().yw() != 0 ) // select only plane in camera
selectable2 = 0;
for ( std::size_t i = 0; i < 6; ++i )
if( &m_selectables[i] == selectable || &m_selectables[i] == selectable2 ){
Selector_add( selector, m_selectables[i] );
selectedPlaneCallback( planes[i] );
}
m_bounds = aabb;
}
void selectReversedPlanes( const AABB& aabb, Selector& selector, const SelectedPlanes& selectedPlanes, const Matrix4& rotation = g_matrix4_identity ){
Plane3 planes[6];
aabb_planes_oriented( aabb, rotation, planes );
for ( std::size_t i = 0; i < 6; ++i )
if ( selectedPlanes.contains( plane3_flipped( planes[i] ) ) )
Selector_add( selector, m_selectables[i] );
}
AABB evaluateResize( const Vector3& translation ) const {
Vector3 min = m_bounds.origin - m_bounds.extents;
Vector3 max = m_bounds.origin + m_bounds.extents;
for ( std::size_t i = 0; i < 3; ++i )
if ( m_bounds.extents[i] != 0 ){
if ( m_selectables[i * 2].isSelected() )
max[i] += translation[i];
if ( m_selectables[i * 2 + 1].isSelected() )
min[i] += translation[i];
}
return AABB( vector3_mid( min, max ), vector3_scaled( vector3_subtracted( max, min ), 0.5 ) );
}
AABB evaluateResize( const Vector3& translation, const Matrix4& rotation ) const {
AABB aabb( evaluateResize( translation_to_local( translation, rotation ) ) );
aabb.origin = m_bounds.origin + translation_from_local( aabb.origin - m_bounds.origin, rotation );
return aabb;
}
Matrix4 evaluateTransform( const Vector3& translation ) const {
AABB aabb( evaluateResize( translation ) );
Vector3 scale(
m_bounds.extents[0] != 0 ? aabb.extents[0] / m_bounds.extents[0] : 1,
m_bounds.extents[1] != 0 ? aabb.extents[1] / m_bounds.extents[1] : 1,
m_bounds.extents[2] != 0 ? aabb.extents[2] / m_bounds.extents[2] : 1
);
Matrix4 matrix( matrix4_translation_for_vec3( aabb.origin - m_bounds.origin ) );
matrix4_pivoted_scale_by_vec3( matrix, scale, m_bounds.origin );
return matrix;
}
};
class ScaleRadius {
ObservedSelectable m_selectable;
public:
static Matrix4& m_model() {
static Matrix4 model;
return model;
}
ScaleRadius( const SelectionChangeCallback& onchanged ) :
m_selectable( onchanged ) {
}
bool isSelected() const {
return m_selectable.isSelected();
}
void setSelected( bool selected ) {
m_selectable.setSelected( selected );
}
void selectPlanes( Selector& selector, SelectionTest& test, const PlaneCallback& selectedPlaneCallback ) {
m_model() = test.getVolume().GetModelview();
Selector_add( selector, m_selectable );
selectedPlaneCallback( Plane3( 2, 0, 0, 0 ) );
}
float evaluateResize( const Vector3& translation ) const {
const float len = vector3_length( translation );
if( len == 0 )
return 0;
Vector3 tra = matrix4_transformed_direction( m_model(), translation );
vector3_normalise( tra );
return tra[0] * len;
}
};
#endif