001package votorola.s.gwt.scene.dum; // Copyright 2010, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import java.util.HashMap;
004import votorola.s.gwt.scene.*;
005import votorola.s.gwt.scene.axial.TriaxialScoping;
006import votorola.s.gwt.scene.feed.*;
007import votorola.s.gwt.stage.poll.*;
008import votorola.g.hold.*;
009
010
011/** A dummy implementation of a scene that situates polls in three dimensions according to
012  * two different hashes of the poll name.  The dummy scene uses {@linkplain
013  * TriaxialScoping triaxial scoping}, q.v. for the format of the 's' scoping switch.
014  */
015public final class DummyScene implements Scene, Hold
016{
017
018
019    /** Constructs a DummyScene.  Call {@linkplain #release release}() when done with it.
020      */
021    public DummyScene()
022    {
023        for( final String pollName: Dummy.POLL_NAME_ARRAY )
024        {
025            polls.put( /*key*/pollName, new MappedPoll( pollName ));
026        }
027    }
028
029
030
031   // ````````````````````````````````````````````````````````````````````````````````````
032   // init for early use
033
034
035    private Spool spool = new Spool1();
036
037
038
039   // ------------------------------------------------------------------------------------
040
041
042    /** Tests whether the specified poll is within the current scope of this scene.
043      */
044    boolean inScope( final MappedPoll poll )
045    {
046        final float x = poll.x();
047        if( x < scoping.xMin() || x > scoping.xMax() ) return false;
048
049        final float y = poll.y();
050        if( y < scoping.yMin() || y > scoping.yMax() ) return false;
051
052        final float z = poll.z();
053        if( z < scoping.zMin() || z > scoping.zMax() ) return false;
054
055        return true;
056    }
057
058
059
060    /** All known polls, keyed by poll name.
061      *
062      *     @see Dummy#POLL_NAME_ARRAY
063      */
064    HashMap<String,MappedPoll> polls() { return polls; }
065
066
067        private final HashMap<String,MappedPoll> polls = new HashMap<String,MappedPoll>();
068
069
070
071    /** The scoping model for this scene.
072      */
073    TriaxialScoping scoping() { return scoping; }
074
075
076        private final TriaxialScoping scoping = new TriaxialScoping( spool );
077
078
079
080   // - H o l d --------------------------------------------------------------------------
081
082
083    public void release() { spool.unwind(); }
084
085
086
087   // - S c e n e ------------------------------------------------------------------------
088
089
090    public boolean inScope( final BiteJS bite )
091    {
092        final PollJS poll = bite.poll();
093        if( poll == null ) return false;
094
095        final String pollName = poll.name();
096        if( pollName == null ) return false;
097
098        final MappedPoll pollM = polls.get( pollName );
099        if( pollM == null ) return false;
100
101        return inScope( pollM );
102    }
103
104
105
106   // ====================================================================================
107
108
109    /** A poll together with its mapping particulars.
110      */
111    static final class MappedPoll
112    {
113
114        private MappedPoll( String _name )
115        {
116            name = _name;
117            x = hash( name, 29 );
118            y = hash( name, 999331 );
119            z = hash( name, 443 );
120        }
121
122
123        private float hash( final String s, final int prime )
124        {
125            int i = 0;
126            for( int c = s.length() - 1; c >= 0; --c )
127            {
128                i = i * prime + s.charAt(c);
129                i |= 0; // truncate to 32 bits when running in browser, where int is actually double
130            }
131
132            float f = Math.abs( i/(float)Integer.MAX_VALUE );
133         // if( f < 0f ) f = 0f;
134         // else if( f >= 1f) f = Math.nextAfter( 1f, 0d ); // exclude 1.0
135         //// 'nextAfter' not available in client, but this is also valid:
136            if( f < 0f || f >= 1f ) f = 0f;
137            return f;
138        }
139
140
141       // --------------------------------------------------------------------------------
142
143
144        /** The name of this mapped poll.
145          *
146          *     @see <a href='pol.xht#poll-id-service' target='_top'>http://reluk.ca/project/_/outcast/pol.xht#poll-id-service</a>
147          */
148        public String name() { return name; }
149
150            private final String name;
151
152
153        /** The x-axis hash of the poll name.
154          *
155          *     @return a number between 0.0 inclusive and 1.0 exclusive.
156          */
157        float x() { return x; }
158
159            private final float x;
160
161
162        /** The y-axis hash of the poll name.
163          *
164          *     @return a number between 0.0 inclusive and 1.0 exclusive.
165          */
166        float y() { return y; }
167
168            private final float y;
169
170
171        /** The z-axis hash of the poll name.
172          *
173          *     @return a number between 0.0 inclusive and 1.0 exclusive.
174          */
175        float z() { return z; }
176
177            private final float z;
178
179
180       // - O b j e c t ------------------------------------------------------------------
181
182
183        public @Override String toString()
184        {
185            return "mapped poll " + name + "(" + x + "," + y + "," + z + ")";
186        }
187
188
189
190    }
191
192
193
194}