sketchucation logo sketchucation
    • Login
    1. Home
    2. jolran
    3. Posts
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info
    Offline
    • Profile
    • Following 0
    • Followers 0
    • Topics 37
    • Posts 1,027
    • Groups 2

    Posts

    Recent Best Controversial
    • RE: Testing Ruby VALUES in C-extension.

      @unknownuser said:

      One thing that cought me off guard is the breakpoints doesn't always work in Release due to compiler optimization. Visual Studio 2013 even has a nice profiler which let you get detailed profiling info of your C++ code. (Should do a quick tutorial on that once.)

      Speaking of debuggers, you know taht from SU2014 you can hook up Ruby debuggers for Ruby code in SketchUp?
      https://github.com/SketchUp/sketchup-ruby-debugger
      It's so much nice to be able to set breakpoints and step than litter the code with puts.

      Well this keeps gets better and better. I did not know that.
      Have been sending puts to ruby console from C++ but that wont work when stepping over code, so this sounds great.

      @unknownuser said:

      You can do something similar C++ std::vector<>.reserve with the Ruby C API by calling rb_ary_new2. You provide the number of final elements and it will allocate enough memory of the object up front.

      Handy! I do a lot of conversions so that will help. I'm not in a mode of optimizing code yet, but suppose best practices are worth picking up at an early stage. πŸ˜„

      I'm experiencing some problems in a loop here. Don't know if you can spot whats going on..
      Have just included the important parts for brevity.

      I have a nested ruby array of points curves. The points are coming from a getter-method-call in a Ruby-class. The @points can be of vary length.
      The problem is that RARRAY(curve_rbPoints)->len always gives the same length, so the code fails..
      Is it because I'm hoisting the variable ?
      I just found this bug so havent tested much yet..

      NOTE: I should mention it get's the correct length 1st time round.
      edit: nope.

      // VALUE curves = [ curve, curve, etc ] // @points = [point3d, point3d, etc]
      	
      	const VALUE get_points = rb_intern("get_points"); //getter method for @points
      	int curves_size = RARRAY(curves)->len; 
      	VALUE curve, curve_rbPoints;
      	int rCrv_len, i;
      	
      	// Inside a loop 
      	for (i = 0; i < curves_size; i++) {
      		
      		curve = rb_ary_entry(curves, i); //My class Ec-curve instance"
      		curve_rbPoints = rb_funcall(curve, get_points, 0); //Gets @points in current curves[index]
      		rCrv_len = RARRAY(curve_rbPoints)->len; //WRONG LENGTH! 
      
      //Nested for loop is coming based on rCrv_len length
      
      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      @unknownuser said:

      Visual Studio 2013 even has a nice profiler which let you get detailed profiling info of your C++ code. (Should do a quick tutorial on that once.)

      Yes that would off course be very nice.

      Realized this thread is staring to get really packed of usefull information. Unfortunately in some time it will be burried in the forum.

      Maybe a C-extension sticky-thread or something similar wouldent hurt. Or maybe this old topic is being used for that purpose ?
      http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=41077%26amp;p=363978

      One might not feel inclined to post silly C++ questions in that old thread.
      Something like "Ruby C extensions for dummies" would be more approprioate. πŸ˜„

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      Hi Thomas.

      For that test I was sending in Ruby array with Sketchup Point3d's to a C++ function.
      That function converted Ruby points to a simple C++ class representing Point3d.
      That same function also converted back the C++ points to Sketchup Points.
      So the return VALUE was a Ruby array of those points.
      Not very useful, but interesting. Got to start somewhere...

      I use vectors as containers in C++, don't know if that is the way to go with Ruby C++-extensions. Vectors seam to be quite used though..
      Do you use maps with Ruby hashes ? I havent tested yet. Vectors seams more flexible.

      What you say about Debug builds seams to hold true.
      I managed to create an implementation for the points-indexing-triangulation I was talking about earlier, and ran that in my plugin compiled. Really fast compared to the Ruby version πŸ˜„
      It still has some quirks and needs refinements, but it was good enough to make a realistic comparison.

      @unknownuser said:

      When I run performance tests in Ruby I tend to call GC.start at the beginning of my profiling to ensure the GC is reset before each test. Otherwise you have no idea when it will trigger

      Ahhh yes. That's smart. Will try that, thanks.

      Edit: Ehh duh! I wasent aware one could create breakpoints and step into the code while debugging in Sketchup. Most useful πŸ’š

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      So I finally managed to bring in 100 000 points convert them to C++ class and then convert back to Sketchup Point3d objects.

      Timing the response back in Alex S-Ruby console clocks ~ 0.50 which is amazingly fast.
      Did some other tests, bloody hell it's blitz.. πŸ˜„

      However if I clock a new "puts time" in the row after the C -callback. it gets increasingly
      slower for each C-functioncall. πŸ˜•
      C-callback response still clocks about the same each time..

      Is it the Ruby garbage collector running ? Adding this amount of points aught to slow Sketchup down, I know..

      So I take it one cannot rely on C-response when things are "ready" to go forth in Ruby ?
      For ex if I expect an array as responce will there be an error in following code.
      Or does not following code execute until the array is ready ?
      (havent tested stuff on so many points yet)

      I don't think I'm creating a memory leak in C++ since I have seen the destructor activate during some tests. And this test is done within 1 function so.. Points in points out..

      Note: I did this in debug-mode(SU 2013), so maybe that affects threading or something.

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Guide Points: Dividing a poly-line in even segments

      To draw edges between 2 curves (step 5)you could paste this into some Ruby code editor.
      Untested..

      a=Sketchup.active_model;b=a.entities;c=a.selection;d={};c.grep(Sketchup;;Edge).each{|e|if cc = e.curve;next if d.has_key?(cc);d[cc] = cc.vertices.collect{|v| v.position };end};if d.length == 2;crvpt1 = d.values[0];crvpt2 = d.values[1];unless crvpt1.length == crvpt2.length;puts "unmatching points from curves";else;gps = b.add_group.entities;for i in (0...crvpt1.length);gps.add_edges(crvpt1[i], crvpt2[i]);end;end;else;puts "Select only 2 curves, please";end 
      

      Edit: Compressed it to 1 line for pasting into Ruby consol..

      posted in SketchUp Discussions
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      Ok, thanks for telling.

      I havent tested it much yet. Will dig in more intensly after the weekend.
      Anyway it seams to be working correctly when just calling functions from the C module in the Ruby console.

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      Yeah, sorry about that.
      I was to exited. πŸ˜„

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      It worked! 😍 To make it simple for myself, I just launched your version..
      I can now launch the debugger and Sketchup's correct version launch, all good to go.

      Just some rookie notes: (things that may be taken for granted by proffesionals πŸ˜‰ )

      • Working with the ruby console in debugg-mode, theres no need to require the module. Just call the C-extension functions.

      * To switch release-version go into Configuration Manager (See pic below). One might think changing Release from 2.0 to 1.8 in the dropdown meny changes the project settings, But it don't(?). Not for me it diden't..

      EDIT: Scratch that. It worked after a project clean.

      Thanks a million Thomthom!! Now it's only the small detail of adding code, left to do.


      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      Exellent!!

      @unknownuser said:

      But if I recall, one of the things where that Ruby define a "inline" macro that conflicts with VS2013.

      Yeah, that's exactly what's happening. That also leads to hundreds of other sub-error messages.

      I give it a try and report back!

      Thanks again. πŸ‘

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      I'm noticing a lot of features lacking in 2010 the more I get into it. So I installed VS 2013.
      (It's quite annoying working with the lack of stl initiallizers in 2010 btw)

      Indeed I get a conversion promt when starting up Hello_world Project.
      So I did convert.

      Thomthom, you said one should alter the Ruby headers.
      Can I have some more info on that? I assume we are talking about ruby.h file included in external dependancies ? Or do you mean doing changes in the project properties.
      In any case I'm a bit lost..

      Sorry about the noob questions 😳

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      @unknownuser said:

      Visual Studio Express editions are free. Are you using any features in Ultimate which Express doesn't have? (VS2013 is really nice - I like it so much more than VS2010.)
      And using the Test Explorer with the Google Runner extensions is also very nice for setting up unit tests.

      Ah, dang! I just looked it up, it's allowed to do commersial products with the express-version. Well, that's good to know though in any case.
      I'm not bound to any features. This all for Sketchup development.
      I might try 2013 later. It took quite a while to install 2010 and not without hassle..

      @unknownuser said:

      Make will work. Any version of SketchUp that support the RubyStartup argument. (I don't recall when that was added.)

      Right. I actually had 2013 and 2014(beta-tester version) installed.
      So I can link to those.

      @unknownuser said:

      You will get complaints about VS not finding debug symbols for SketchUp. You can ignore that. for mscms.dll, that's a MS component. If you enable the Microsoft Symbols Server it will load most of the PDBs:

      Great! 1 step closer. I still get the PDB complaint but later, but VS seams to be loading a lot of symbols in the background now.
      Should I locate the project directory in the "symbol pdb locations" ?
      I don't know what I doing..

      @unknownuser said:

      Ahhh, Greece. Man, that would have been nice...

      You can't make it? 😞

      Doubt it. This "programming lifestyle" is limiting my budget.

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      @unknownuser said:

      Btw guys, have you heard of DevCamp we're doing in Greece at the end of September?

      Ahhh, Greece. Man, that would have been nice...

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      Wooah! You seam to have gotten far into this C++ business. Well done!

      @unknownuser said:

      I'd try to wrap that whole calculation in a C extension. C++ extension that is.

      I should probably have mentioned that these containers are long lived. The may exist during the whole program.
      I do have some more special methods for dealing with other objects(node-editor project) but for this purpouse it would be most intersting to se an example on how you do things in C++ in an isolated case.
      In fact, It may be interesting for the whole communitive. (as long as we can use/abuse/expand on it in our projects, that is πŸ˜‰ )

      @unknownuser said:

      Indeed. I have VS2013. You can load the project just fine, but if you want to use it as-as you must decline to upgrade it.
      If you do choose to upgrade you must modify the Ruby headers as they are not configured to VS2013. That's what I do for my projects now because I like C++11 so much with it's lambdas, smart pointers, ranged for loops and other goodnesses.

      Ah ok. Well My brother gave me a seat of VS 2010 ULTIMATE. Can't really afford upgrading that..$$$$ 😲

      @unknownuser said:

      It is in fact set up to SU2014. When you click run it should launch SketchUp 2014 and a supplementary RB script should load the c extension for you.

      Edit: That doesent happend for me. I get linking errors. I'll look into it more. I wasent aware I could debugg it like that. Sounds a bit more flexible πŸ˜„
      Ehh. 😳 I don't have 2014 either.. Not Pro anyway. Make will work ?

      edit: I linked to Sketchup8 which it now seams to find, but I'll keep getting errors.
      'SketchUp.exe': Loaded 'C:\Windows\SysWOW64\mscms.dll', Cannot find or open the PDB file
      I think I may have linked the RubyStartup "$(SolutionDir)Ruby$(Configuration).rb" file wrong..

      For the rest of the information. πŸ‘
      Very informative post this is turning out to be!

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      @unknownuser said:

      Why are you looking for a key by value? Sounds inverted.

      Yeah πŸ˜„ I may very well have a design-flaw on my hands..

      But it is a well-used pattern.

      I'm using a Hash with uniq points. And then building different geometries from those points for rendering Open_Gl Triangles and edges.
      Normally the ordering of points for face-triangles VS face-edges are different.
      So one ends up with 2 arrays of points. 1 for GL_Triangles , 2 for GL_edges.

      NOW, Instead of having 3 Containers filled with points. You have only 1 HASH. And 2 Arrays of integers that act as lookups-key to the Hash.
      In terms of space this is more lightweight. But The greatest advantage is when for ex Transforming all that geometry. I only have to transform the uniq points.

      Using Polygonmesh for this would be better, but one cannot update or purge a polygonmesh.
      And I would need to build the 2 arrays anyway, so I have my own class for this purpose.

      There are some mechanics for sorting and then I have to lookup the key from a value.
      Hope that makes the subject clearer..

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      Awesome answer man! Very rich post πŸ‘

      I absorb your recommendations, comments are not necessary πŸ˜„

      (nice links btw, now I finally understand Log(n)!).

      Regarding the Hash lookups. Yes they are fast if using the key. But if looking for the key from a value it's a bit worse. And in my case I see no way around avoiding that lookup.

      I'm doing vertex indexing.
      http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-9-vbo-indexing/
      Where I sometimes have to insert new uniq points, and get the index from a point when sorting the indexed arrays by some algoritm.

      @unknownuser said:

      If you are diving into Ruby C Extensions I would recommend using our GitHub projects which is in fact a C++ Extension.

      I am using the C++ hello_world tutorial as staring point, and expanding on that.
      How do you debugg BTW ? It's a bit enoying haiving to quit Sketchup when pasting in .so files. Windows don't allow me to copy and paste over so files that are in use by Sketchup 😞 One cannot unload dll's, right ?
      There is a debug and release modes(I'm in VS 2010 Ultimate BTW) but I can't launch debugger with SU8 and debug 1.8.
      The original project seams to be linked to Sketchup 2013(which I don't have) so I tried to link to SU8.exe to no avail.

      What I've seen the recommendation is to use VS 2010 (if windows). Unfortunately VS 2010 lack some of the nice C++11 features, but I guess we can live with that.
      A bit difficult to follow some newer C++ tutorials though.

      @unknownuser said:

      Constructors and destructor are your friend

      Yeah, but scary subject. I followed some tutorials here:
      http://www.microsoftvirtualacademy.com/Content/ViewContent.aspx?et=5730%26amp;m=5723%26amp;ct=23224
      recommending RAII with uniq pointers inside try and catch block. So in our case one could use that to ensure longjumps to Ruby if an Exeption ?
      (Way over my head at this stage, but interesting.)
      I havent found much info about dealing with objects in C++ and memory handling in C-extensions yet.
      I presume the Alloc_n -> xfree trick does not apply in this case ?
      Or maybe it's the same in C++ if using a struct and not Class.
      A struct must be perfect choice for Point3d in any case ?

      Once again. Thanks a lot for that post. Very useful.

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      @unknownuser said:

      Something tells me SU2015 could have support for plugins in C++. Would be awesome.

      +1

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      @unknownuser said:

      Don't think there is much difference calling a method from a Ruby script or doing the same call from C.

      That is very good to know!

      @unknownuser said:

      What made a difference was that I converted the array of points into C structures and then made all my comparisons and calculations, then converted the result back to Ruby.

      I have looked a lot at your C codes, (from Bitbucket). I was wondering about that part.
      From an untrained eye it looks rather cumbersome to convert to struct and then back again, isent there a huge cost in doing so ?

      What you are saying it's the opposite ? I wouldent mind doing that. It would be a consistent way of dealing with points.
      Needless to say, I don't really understand how things work in C-extensions yet..

      @unknownuser said:

      Are you trying to use 3d points as hash

      Hmm, No. I use a Hash as container for points. like this { 0 => pt, 1 => pt etc }
      This Hash is then compared/used against arrays of indexes(integers) referring to this Hash.
      So I need to lookup the key(index) from value(pt) often when dealing with these arrays in different methods.

      The spontanious reaction would be to use an Array instead of Hash for this, but after some "interesting" benchmarks when using array.index(object) I decided to use a Hash. πŸ˜„
      I havent tried a set yet though, might be better candidate..
      I don't know which one is simpler to work with in C.

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Wind generator

      @unknownuser said:

      That's what I was talking about if you didn't comprehend me πŸ˜„

      Looks like I did not. My bad. πŸ˜„

      I know nothing about the subject, in particular..

      posted in Corner Bar
      jolranJ
      jolran
    • RE: Testing Ruby VALUES in C-extension.

      Hi Thomthom.

      I suspected I was comparing adresses, which deceivingly gave the correct answer in this particular case. But will fail (?) if comparing to equal objects from different address.

      @unknownuser said:

      As far as I can tell you must use rb_funcall to invoke the == operator of the Geom::Point3d class.

      I will stick to that.
      Does it slow down the code much invoking Ruby methods in C ?
      I have that impression from reading topics in here..
      It must be complicated to avoid doing so in many cases.

      The main reason for thsi was that I was planning on building a hash.index(object) function
      in C. It's rather slow in Ruby.
      (Or any function that will get the key in a Hash from a value).

      Anyway, thanks a lot for the help! I will read the links carefully now.

      posted in Developers' Forum
      jolranJ
      jolran
    • RE: Wind generator

      Yeah, probably. But I thought the question was how to slow down the RPM in strong winds..

      Anyway, if the station is rotatable/hinged, when folded the blades would act as parachute directing the station in wrong direction, perhaps..

      posted in Corner Bar
      jolranJ
      jolran
    • 1 / 1