source: trunk/nv/interface/data_channel.hh @ 456

Last change on this file since 456 was 456, checked in by epyon, 10 years ago
  • mesh_creator minor cleanup - need more
  • data channel cloning
  • removed debug msg
File size: 5.8 KB
Line 
1// Copyright (C) 2012-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#ifndef NV_INTERFACE_DATA_CHANNEL_HH
8#define NV_INTERFACE_DATA_CHANNEL_HH
9
10#include <nv/common.hh>
11#include <nv/stl/vector.hh>
12#include <nv/interface/data_descriptor.hh>
13
14namespace nv
15{
16
17        struct raw_data_channel
18        {
19                raw_data_channel( raw_data_channel&& other )
20                {
21                        m_data = other.m_data;
22                        m_size = other.m_size;
23                        m_desc = other.m_desc;
24                        other.m_data = nullptr;
25                        other.m_size = 0;
26                }
27
28                raw_data_channel& operator=( raw_data_channel&& other )
29                {
30                        if ( this != &other )
31                        {
32                                if ( m_data != nullptr ) delete[] m_data;
33                                m_data = other.m_data;
34                                m_size = other.m_size;
35                                m_desc = other.m_desc;
36                                other.m_data = nullptr;
37                                other.m_size = 0;
38                        }
39                        return *this;
40                }
41
42                const data_descriptor& descriptor() const { return m_desc; }
43                uint32 element_size() const { return m_desc.element_size(); }
44                uint32 size() const { return m_size; }
45                uint32 raw_size() const { return m_size * m_desc.element_size(); }
46                const uint8* raw_data() const { return m_data; }
47
48                // TODO: constexpr compile-time cast check?
49                template < typename Struct >
50                const Struct* data_cast() const
51                {
52                        return reinterpret_cast<const Struct*>( m_data );
53                }
54
55                // TODO: constexpr compile-time cast check?
56                template < typename T >
57                const T& extract( uint16 slot_index, uint32 index ) const
58                {
59                        NV_ASSERT( slot < m_desc.size(), "Bas slot passed to extract!" )
60                        return *reinterpret_cast<const T*>( m_data + m_desc.element_size()*index + m_desc[slot_index].offset );
61                }
62
63                bool has_slot( slot vslot ) const { return m_desc.has_slot( vslot ); }
64
65                ~raw_data_channel()
66                {
67                        if ( m_data != nullptr ) delete[] m_data;
68                }
69
70        protected:
71
72                template < typename Struct >
73                friend class data_channel_access;
74                friend class raw_data_channel_access;
75                friend class data_channel_creator;
76                friend class data_channel_set;
77
78                raw_data_channel() : m_data( nullptr ), m_size( 0 ) {}
79                raw_data_channel( const raw_data_channel& ) = delete;
80                raw_data_channel& operator=( const raw_data_channel& ) = delete;
81
82                uint8*          m_data;
83                uint32          m_size;
84                data_descriptor m_desc;
85        };
86
87        class data_channel_set
88        {
89        public:
90                friend class data_channel_set_creator;
91                friend class raw_data_channel_access;
92                friend class mesh_nodes_creator;
93
94                typedef const raw_data_channel* const_iterator;
95
96                data_channel_set( data_channel_set&& other )
97                {
98                        for ( uint32 c = 0; c < other.m_size; ++c )
99                                m_channels[c] = move( other.m_channels[c] );
100                        m_size       = other.m_size;
101                        m_parent_id  = other.m_parent_id;
102                        m_transform  = other.m_transform;
103                        m_name       = other.m_name;
104                        other.m_size = 0;
105                }
106
107                data_channel_set& operator=( data_channel_set&& other )
108                {
109                        if ( this != &other )
110                        {
111                                for ( uint32 c = 0; c < other.m_size; ++c )
112                                        m_channels[c] = move( other.m_channels[c] );
113                                m_size      = other.m_size;
114                                m_parent_id = other.m_parent_id;
115                                m_transform = other.m_transform;
116                                m_name      = other.m_name;
117                                other.m_size = 0;
118                        }
119                        return *this;
120                }
121
122                size_t size() const { return m_size; }
123
124                const raw_data_channel* get_channel( size_t index ) const
125                {
126                        if ( m_size > index ) return &m_channels[index];
127                        return nullptr;
128                }
129
130                const raw_data_channel* get_channel( slot s ) const
131                {
132                        for ( uint32 c = 0; c < m_size; ++c )
133                                if ( m_channels[c].has_slot( s ) )
134                                return &m_channels[c];
135                        return nullptr;
136                }
137
138                template < typename Struct >
139                const raw_data_channel* get_channel() const
140                {
141                        data_descriptor compare;
142                        compare.initialize<Struct>();
143                        for ( uint32 c = 0; c < m_size; ++c )
144                        {
145                                if ( m_channels[c].descriptor() == compare )
146                                {
147                                        return &m_channels[c];
148                                }
149                        }
150                        return nullptr;
151                }
152
153                size_t get_channel_size( slot s ) const
154                {
155                        const raw_data_channel* channel = get_channel( s );
156                        return channel ? channel->size() : 0;
157                }
158
159                size_t get_channel_size( size_t channel ) const
160                {
161                        if ( m_size > channel ) return m_channels[channel].size();
162                        return 0;
163                }
164
165                template < typename Struct >
166                size_t get_channel_size() const
167                {
168                        data_descriptor compare;
169                        compare.initialize<Struct>();
170                        for ( uint32 c = 0; c < m_size; ++c )
171                        {
172                                if ( m_channels[c].descriptor() == compare )
173                                {
174                                        return m_channels[c].size();
175                                }
176                        }
177                        return 0;
178                }
179
180                template < typename VTX >
181                const VTX* get_channel_data() const
182                {
183                        data_descriptor compare;
184                        compare.initialize<VTX>();
185                        for ( uint32 c = 0; c < m_size; ++c )
186                        {
187                                if ( m_channels[c].descriptor() == compare )
188                                {
189                                        return m_channels[c].data_cast<VTX>( );
190                                }
191                        }
192                        return nullptr;
193                }
194
195                int get_channel_index( slot s ) const
196                {
197                        for ( uint32 c = 0; c < m_size; ++c )
198                                if ( m_channels[c].has_slot( s ) )
199                                return int( c );
200                        return -1;
201                }
202
203                data_descriptor get_interpolation_key() const
204                {
205                        data_descriptor result;
206                        for ( uint32 c = 0; c < m_size; ++c )
207                        {
208                                for ( const auto& cslot : m_channels[c].descriptor() )
209                                        if ( cslot.vslot != slot::TIME )
210                                        result.push_slot( cslot.etype, cslot.vslot );
211                        }
212                        return result;
213                }
214
215                const_iterator begin() const { return &m_channels[0]; }
216                const_iterator end()   const { return &m_channels[ m_size ]; }
217
218                shash64 get_name() const { return m_name; }
219                sint16 get_parent_id() const { return m_parent_id; }
220                const mat4& get_transform() const { return m_transform; }
221
222        protected:
223
224                data_channel_set()
225                {
226                        m_size      = 0;
227                        m_parent_id = -1;
228                }
229
230                raw_data_channel m_channels[4];
231
232                uint32  m_size;
233                shash64 m_name;
234                sint16  m_parent_id;
235                mat4    m_transform;
236        };
237
238}
239
240#endif // NV_INTERFACE_DATA_CHANNEL_HH
Note: See TracBrowser for help on using the repository browser.