Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Data sequence

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Data sequence

    Dear reader,

    I'm new to java programming. I have made an application that shows a 3 dimensional object using glDrawElements, a strip of triangles, and an index buffer. the index buffer is a lot of numbers with some nice structure. I was wondering if i could make the index buffer another way using sequences to decrease the application size. Would it be faster in execution time? For example the sequence {1,2,3,4,5,6} can be made in matlab (a math solving program) using the simple statement 1:6. Do i need to import another class for it? an example would be very helpful.

    Thanks alot


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Data sequence

    Write a short Java program with a main() method so that it can be run from the command line that demonstrates the
    some nice structure
    doing something (maybe just printing itself out - but definitely not doing any GL!), so that we can get a better idea of what you mean.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Data sequence

    Here is a copy of part of the application that creates the indices buffer.
    It is not printed to screen yet because i don't know how to do that.

    public class main {


    // index buffer
    private short[] indices = {0, 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,0,1,
    1,32,3,34,5,36,7,38,9,40,11,42,13,44,15,46,17,48,1 9,50,21,52,23,54,25,56,27,58,29,60,31,62,1,32,
    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,32,3 3,
    64,65,66,67,68, 33,64,35,68,37,70,39,72,41,74,43,76,45,78,47,80,49 ,82,51,84,53,86,55,88,57,90,59,92,61,94,63,96};
    }

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Data sequence

    I don't know if you could make your application any smaller - I guess (using your example above) you could do less (or more?) typing with something like
    NiceStructure ns = new NiceStructure().add(0, 31)
     .add(0, 1).add(1).add(32).add(3).add(34).add(5).add(36).add(7).add(38).add(9)
     .add(40).add(11).add(42).add(13).add(44).add(15).add(46).add(17).add(48).add(19)
     .add(50).add(21).add(52).add(23).add(54).add(25).add(56).add(27).add(58).add(29)
     .add(60).add(31).add(62).add(1).add(32)
     .add(32, 63)
     .add(33).add(64).add(35)
     .add(68).add(37).add(70).add(39).add(72).add(41).add(74).add(43).add(76).add(45)
     .add(78).add(47).add(80).add(49).add(82).add(51).add(84).add(53).add(86)
     .add(55).add(88).add(57).add(90).add(59).add(92).add(61).add(94).add(63).add(96);
    But you have some fairly long runs of non-sequences (or non-simple sequences) in your example. If those long runs of numbers can be generated by simple rules, you could add methods onto a class like NiceStructure which would build an internal short for you. I just wrote this class out of interest and can re-create your original sequence with this sequence of methods (I didn't type them all in, I copy+paste+regex them). However, I think it actually ends up being less readable!

    You could reduce that mess a little bit with a add(int... ) method that employed varargs:
    http://download.oracle.com/javase/1,...e/varargs.html
    Last edited by Sean4u; August 23rd, 2011 at 10:22 AM. Reason: varargs

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Data sequence

    There you go: version using varargs:
    NiceStructure ns = new NiceStructure().add(0, 31).add(0, 1)
     .add(1, 32, 3, 34, 5, 36, 7, 38, 9, 40, 11, 42, 13, 44, 15, 46, 17, 48, 19, 50, 21, 52, 23, 54, 25, 56, 27, 58, 29, 60, 31, 62, 1, 32)
     .add(32, 63)
     .add(33, 64, 35, 68, 37, 70, 39, 72, 41, 74, 43, 76, 45, 78, 47, 80, 49, 82, 51, 84, 53, 86, 55, 88, 57, 90, 59, 92, 61, 94, 63, 96);

  6. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Data sequence

    And just one more - those sequences are kind of 2-step arithmetic progressions, so something like this would capture the sequence:
    NiceStructure ns = new NiceStructure().add(0, 31).add(0, 1)
     .addArithmeticSequence(1, 62, new int[]{31, -29})
     .add(1).add(32)
     .add(32, 63)
     .add(32, 3, 3)
     .add(64, 68)
     .add(33, 64, 35, 68)
     .addArithmeticSequence(37, 96, new int[]{33, -31});

    How do you generate those numbers? It's obviously possible to reverse-engineer the patterns of integers in this case to get some reduction in code, but perhaps you should be either writing the pattern-generating code in Java, or you should be exporting the data from the creating application to a file and importing it in your Java code.

  7. The Following User Says Thank You to Sean4u For This Useful Post:

    street_missile (August 23rd, 2011)

  8. #7
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Data sequence

    Perhaps I've misunderstood the question.
    Would it be faster in execution time?
    The answer is "no". :imatwit:

  9. #8
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Data sequence

    Thanks for the quick replies. I'm going to play with that .add to see if i can get my code like that.
    With the execution time question, i mean if it is better to have a large amount of data in the memory, ready for processing, or if it is better to destroy and recreate the data each time it is needed.

  10. #9
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Data sequence

    better to have a large amount of data in the memory, ready for processing
    Absolutely. I think you have to balance maintainability against performance sometimes, and other times there are good designs that address both. java.util.regexp.Pattern is a great example. You typically provide a convoluted regular expression as a String, but create an optimised pattern-matching object with Pattern.compile() for use again and again. You wouldn't want to parse a complex regular expression every time you used it.

    The performance question greatly depends on how your short array is used. Is it passed once to the GL library and then referred to by handle? In which case, there's no point in optimising it at all. If it's frequently passed in as an argument to something, say at display-refresh rates, then having an object that prepared the short array in the way that NiceStructure does, will help. I implemented mine with a .get() method that returns the finished array. The way I've written some parts of NiceStructure would burn through memory like crazy - I resize the array for every number in addArithmeticSequence - but if the use case is "prepare once, then used short array over and over", it's not unreasonable.

    How do you create the sequence of ints anyway? Do you dream them up and write them down like one of those guys who can write symphonies but can't tie their shoelaces, or is it generated by some other program?

  11. The Following User Says Thank You to Sean4u For This Useful Post:

    street_missile (August 26th, 2011)

  12. #10
    Junior Member
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Data sequence

    Quote Originally Posted by Sean4u View Post
    How do you create the sequence of ints anyway? Do you dream them up and write them down like one of those guys who can write symphonies but can't tie their shoelaces, or is it generated by some other program?
    I write the surface as a mathematical expression. The surface is first parametrized in a u and v coordinate, similar to the t (time) coordinate in a curve (1 dimensional). I evaluate the expression of the surface at different points with Matlab (a mathematical application), which result in a list of points in space. These points can be used for the vertices. I do the triangulation by hand since that works pretty well. You see a repetition in the sequence pretty fast, and when you discovered the sequence, you can automate it. Matlab has a function called delaungy, but that does not work very well for me.

    Thanks for asking, i don't know how it's normally done, if there is a special program for it, but i like to do it this way since it gives me control over the triangulation, and it is robust.

  13. The Following User Says Thank You to street_missile For This Useful Post:

    Sean4u (August 26th, 2011)

  14. #11
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Data sequence

    Thanks for asking
    Part of my motivation for asking was that if you were generating those sequences using an algorithm or heuristic yourself, perhaps the best way to 'compress' their representation in Java would be to code your technique directly. If you're using a combination of matlab and 'eye' to get the sequences, maybe you're right and it's more trouble than it's worth!

Similar Threads

  1. Returning the equilibrium index in a sequence of integers
    By thanasisk in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 3rd, 2013, 02:20 PM
  2. Data is covered by latter data
    By ljz1031 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 15th, 2011, 08:04 PM
  3. Replies: 2
    Last Post: June 15th, 2011, 03:49 PM
  4. Replies: 1
    Last Post: June 11th, 2011, 05:39 AM
  5. Replies: 3
    Last Post: October 19th, 2010, 03:49 PM

Tags for this Thread