AI commenting

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

AI commenting

Postby Bird » Sun Feb 19, 2023 7:40 pm

I asked ChatGPT Plus to generate comments for the ndAabbPolygonSoup::CalculateFaceMaxDiagonal() funtion in ndAabbPolygonSoup.cpp.

Here's what it spit back almost immediately

Code: Select all
// Given an array of vertices and an array of indices, calculates the maximum diagonal length of each face in the mesh
// Returns the maximum diagonal length among all faces in the mesh
ndFloat32 ndAabbPolygonSoup::CalculateFaceMaxDiagonal (const ndVector* const vertex, ndInt32 indexCount, const ndInt32* const indexArray) const
{
   // Initialize variables
   ndFloat32 maxSize = ndFloat32 (0.0f);
   ndInt32 index = indexArray[indexCount - 1];
   ndVector p0 (vertex[index]);

   // Iterate over all indices in the index array
   for (ndInt32 i = 0; i < indexCount; ++i)
   {
      // Get the current index and the vertex associated with it
      ndInt32 index1 = indexArray[i];
      ndVector p1 (vertex[index1]);

      // Calculate the direction of the current edge
      ndVector dir (p1 - p0);

      // Check that the direction vector's fourth component is 0
      ndAssert (dir.m_w == ndFloat32 (0.0f));

      // Normalize the direction vector
      dir = dir.Normalize();

      // Initialize variables to track the maximum and minimum dot products of the direction vector with each vertex
      ndFloat32 maxVal = ndFloat32 (-1.0e10f);
      ndFloat32 minVal = ndFloat32 ( 1.0e10f);

      // Iterate over all indices in the index array again
      for (ndInt32 j = 0; j < indexCount; ++j)
      {
         // Get the current index and the vertex associated with it
         ndInt32 index2 = indexArray[j];
         ndVector q (vertex[index2]);

         // Calculate the dot product of the direction vector and the current vertex
         ndFloat32 val = dir.DotProduct(q).GetScalar();

         // Update the maximum and minimum dot products
         minVal = ndMin(minVal, val);
         maxVal = ndMax(maxVal, val);
      }

      // Calculate the size of the face diagonal and update the maximum size if necessary
      ndFloat32 size = maxVal - minVal;
      maxSize = ndMax(maxSize, size);

      // Update the starting vertex for the next edge
      p0 = p1;
   }

   // Return the maximum diagonal length among all faces in the mesh
   return maxSize;
}

Bird
 
Posts: 623
Joined: Tue Nov 22, 2011 1:27 am

Re: AI commenting

Postby Bird » Sun Feb 19, 2023 7:45 pm

I asked it if it could improve the efficiency of this function

======================
Certainly! Here are some potential improvements to make the CalculateFaceMaxDiagonal function more efficient:

Reuse the direction vector instead of recalculating it for each edge. Since each edge shares a vertex with the previous edge, you can reuse the direction vector from the previous edge for the current edge.

Cache the dot products of each vertex with the direction vector to avoid recomputing them multiple times. Since each vertex is shared by multiple edges, you can cache the dot product for each vertex once and reuse it for each edge that shares that vertex.

Here's an updated version of the function that incorporates these improvements:

Code: Select all
ndFloat32 ndAabbPolygonSoup::CalculateFaceMaxDiagonal (const ndVector* const vertex, ndInt32 indexCount, const ndInt32* const indexArray) const
{
   // Initialize variables
   ndFloat32 maxSize = ndFloat32 (0.0f);
   ndInt32 index = indexArray[indexCount - 1];
   ndVector p0 (vertex[index]);

   // Initialize variables to store the dot product cache
   std::vector<ndFloat32> dotProductCache(indexCount, 0.0f);

   // Compute the dot product for the first vertex
   dotProductCache[indexCount - 1] = p0.DotProduct(m_PlaneNormal).GetScalar();

   // Iterate over all indices in the index array
   for (ndInt32 i = 0; i < indexCount; ++i)
   {
      // Get the current index and the vertex associated with it
      ndInt32 index1 = indexArray[i];
      ndVector p1 (vertex[index1]);

      // Calculate the direction of the current edge
      ndVector dir = (p1 - p0).Normalize();

      // Initialize variables to track the maximum and minimum dot products of the direction vector with each vertex
      ndFloat32 maxVal = dotProductCache[i];
      ndFloat32 minVal = dotProductCache[i];

      // Iterate over all indices in the index array again
      for (ndInt32 j = i + 1; j < indexCount; ++j)
      {
         // Get the current index and the vertex associated with it
         ndInt32 index2 = indexArray[j];
         ndVector q (vertex[index2]);

         // Calculate the dot product of the direction vector and the current vertex
         ndFloat32 val = q.DotProduct(dir).GetScalar();

         // Update the maximum and minimum dot products
         minVal = ndMin(minVal, val);
         maxVal = ndMax(maxVal, val);

         // Cache the dot product for the current vertex
         dotProductCache[j] = val;
      }

      // Calculate the size of the face diagonal and update the maximum size if necessary
      ndFloat32 size = maxVal - minVal;
      maxSize = ndMax(maxSize, size);

      // Update the starting vertex for the next edge and its cached dot product
      p0 = p1;
      dotProductCache[i] = p1.DotProduct(dir).GetScalar();
   }

   // Return the maximum diagonal length among all faces in the mesh
   return maxSize;
}

Bird
 
Posts: 623
Joined: Tue Nov 22, 2011 1:27 am

Re: AI commenting

Postby Julio Jerez » Sun Feb 19, 2023 9:08 pm

the comments are spot on, but I believe it made a big mistake here

Code: Select all
for (ndInt32 j = i + 1; j < indexCount; ++j)


that should be

Code: Select all
for (ndInt32 j = 0; j < indexCount; ++j)


or it will report a false max lenght

also thsi comment is incorrect
Reuse the direction vector instead of recalculating it for each edge. Since each edge shares a vertex with the previous edge, you can reuse the direction vector from the previous edge for the current edge.


the direction vector is only computed in the outer loop.
the optimized code, will report smaller sized with will cause more clips than necessary in the convex vs flat polygons collisions.

but I have to say it in quite good, and can only get better.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: AI commenting

Postby Bird » Sun Feb 19, 2023 9:23 pm

Yeah, definitely not quite good enough to be trusted yet but pretty amazing nonetheless. I wish I could pass it the contents of my whole code base for commenting. :mrgreen:

I tried it on some of my OptiX cuda kernels and it had no problem with those either.
Bird
 
Posts: 623
Joined: Tue Nov 22, 2011 1:27 am

Re: AI commenting

Postby Julio Jerez » Sun Feb 19, 2023 9:35 pm

I wonder what happen if someone ask ChatGtp to creat a Chatgtp program that is 0.0001 % better than itself.

I think I saw thato movie before. Here is a preview.
https://youtu.be/_Wlsd9mljiU

And it was a really bad movie.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: AI commenting

Postby Bird » Sun Feb 19, 2023 9:46 pm

I asked "create a ChatGTP program that is 0.0001 % better than itself"

=========================
As an AI language model, I don't have the ability to modify my own code or programming. However, I can continue to improve through ongoing updates and advancements in AI technology.

To ensure I am always performing at my best, my developers regularly update my programming and training data with new information and algorithms. This allows me to continually learn and improve over time.

Additionally, users can help improve my responses by providing feedback on my performance, correcting any errors or inaccuracies, and asking me a variety of questions to help expand my knowledge and understanding.

So, while I can't make specific improvements to myself on command, I am constantly being updated and refined to provide the best possible responses to users.
Bird
 
Posts: 623
Joined: Tue Nov 22, 2011 1:27 am

Re: AI commenting

Postby Julio Jerez » Sun Feb 19, 2023 10:01 pm

That answer remind me of these kind of answered.
https://youtu.be/VSrEEDQgFc8

I can only imagine when the conspiracy theorist of the internet gets a load of ChatGtp and start using to generate Conspiracies that are at least coherent.

You can call me old faction, but I do not think anything good can come out of that program.

I think the public would probably be better served if they decriminalize crack cocaine, rather than putting that thing at the disposal of a social media world that now runs on click bait.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: AI commenting

Postby JernejL » Mon Feb 20, 2023 9:03 am

Chatgpt offers a generator of good, creative starting ideas.

It's certainly not somthing to completely trust, but has it's own use.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: AI commenting

Postby JoshKlint » Thu Jun 08, 2023 1:29 pm

If you use the API you can send it any amount of text.
JoshKlint
 
Posts: 163
Joined: Sun Dec 10, 2017 8:03 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 38 guests